Inlines in admin

38 views
Skip to first unread message

Emanuel

unread,
Apr 7, 2014, 9:04:41 AM4/7/14
to django...@googlegroups.com
Hi all!

I'm have the following models:

Class A(models.Model):
     pass


Class Z(models.Model):
    pass


Class B(models.Model):
      a = models.ForeignKey(a)
      z = models.ForeignKey(Z)

       def __unicode__(self):
          return self.z


Class C(models.Model):   
     b = models.ForeignKey(B)

   
I Want in the admin add A,B and C objects.

I know how to put inline objects of B in A. Everything works as expected. But I want to create new C's in the same page. Something like having C as an Inline of B, beeing B an Inline of A. It could appear all in popups...

I dont want the user have to select B when adding C. If it was all in the same page I always have object A present and user just have to add C, without having trouble in knowing what it was B in the first place

Thanks


Marc Aymerich

unread,
Apr 7, 2014, 5:52:15 PM4/7/14
to django-users
On Mon, Apr 7, 2014 at 3:04 PM, Emanuel <costav...@gmail.com> wrote:
> Hi all!
>
> I'm have the following models:
>
> Class A(models.Model):
> pass
>
>
> Class Z(models.Model):
> pass
>
>
> Class B(models.Model):
> a = models.ForeignKey(a)
> z = models.ForeignKey(Z)
>
> def __unicode__(self):
> return self.z
>
>
> Class C(models.Model):
> b = models.ForeignKey(B)
>
>
> I Want in the admin add A,B and C objects.
>
> I know how to put inline objects of B in A. Everything works as expected.
> But I want to create new C's in the same page. Something like having C as an
> Inline of B, beeing B an Inline of A. It could appear all in popups...

nested inlines are not currently supported. A work around can be using
a readonly field on B inline that points to a B change view with C
inlines


class BInline(admin.TabularInline):
model = B
readonly_fields = ('c_link',)

def c_link(self, instance):
if not instance.pk:
return ''
url = reverse('admin:app_c_change', args=(instance.pk,))
popup = 'onclick="return showAddAnotherPopup(this);"'
return '<a href="%s" %s>%s</a>' % (url, popup, instance)
c_link.allow_tags=True


class CInline(admin.TabularInline):
model = C


class AModelAdmin(admin.ModelAdmin):
inlines = [BInline]


class BModelAdmin(admin.ModelAdmin):
inlines = [Cinline]

Emanuel

unread,
Apr 8, 2014, 8:46:24 AM4/8/14
to django...@googlegroups.com
It solves the problem.

Thanks
Reply all
Reply to author
Forward
0 new messages