0

I'm still stuck with the inline Tree-like-eiditing of related models on same page. I've got three models, A, B and C.

Class A

Class B
    fb = foreignkey(A)

Class C
    fc = foreignkey(B)

In admin.py I'm doing something like

AdminA
    inlines = [inlineB]

AdminB
    inlines = [inlineC]

I want that when I edit/add model A, I should be able to add ModelB inline, and add Model B's related Model C entries. I was trying out inlineformsets, but can't find out how to use them for my purpose. Moreover, I found this old discussion on same problem. But again, since I'm new to Django, I don't know how to make it work.

Community
  • 1
  • 1
Neo
  • 13,179
  • 18
  • 55
  • 80
  • _What did you actually try so far?_ You made a related question 12 hours earlier, in which you said that Model B defined merely the relation between A and C. I once had a similar set-up with models from django-tagging and it basically worked using a `GenericInlineAdminForm` for `TaggedItem`, which also mainly defines the relation between tags, tagged objects and content types. However, you cannot expect to get a helpful answer here if you state only rather abstract questions. – Bernd Petersohn Oct 07 '10 at 13:39
  • 12 hours ago, yes B was an abstract class. But not anylonger, I made some changes to the class. Earlier I was hoping to find some built in solution, like the popup which appears if you want to create a foreginkey object. Now I'm trying to create an inlineformset kind of structure. So in Inline row, I'm hoping that apart from fields of B, I'm able to add instances class C objects. So looking for a customized formset solution using modelforms, inlineformsets etc. Infact my problem is exactly the same as the one in the link i've mentioned. Can you fill me with the details of its solution? – Neo Oct 07 '10 at 13:53
  • 1
    Please take a look at http://docs.djangoproject.com/en/1.2/ref/contrib/admin/#inlinemodeladmin-objects. If you define an InlineAdminForm for model B, e.g. derived from admin.TabularInline, you should get a form display that provides **+**-buttons which should enable you to create instances of C within a popup. If that doesn't work, check your foreign keys. The question you linked to basically has reasonable answers to your problem. – Bernd Petersohn Oct 07 '10 at 14:34
  • Bernd, thanx for replying. But I guess u've misunderstood the problem. I need to edit/add C instances, from the add/edit page of A. Obviously, edit/add C as inline of model B works with "+", just as you mentioned. But this is a tree-problem, edit C in page of A. I wrote the problem in an abstract manner so as to capture the gist of the problem, which was Adding a new instance of A and simultaneously add B( and its C objects). The original problem, has a nice solution, but many parts are incomplete, especially since it used older django. I'll post a complete solution once I figure it out. – Neo Oct 07 '10 at 15:00
  • Not quite. I meant a tabular inline display of B within the page for A with the possibility to add instances of C from B's inline display. In my case, B was a list of tags for an article A with selection boxes to choose tags from exiting ones and plus buttons to create and assign new tags (C). However, an inline display for C within the page for A - bypassing any display of B - is probably more difficult to achieve. – Bernd Petersohn Oct 07 '10 at 16:06

1 Answers1

1

Its a bit odd answering your own question, but hey nobody else stepped up. And thanks to Bernd for pointing me in right direction. The solution required making an intermediary model. Class BC in my case.

class A(models.Model):                                        
a = models.IntegerField()                                 


class B(models.Model):                                        
    fb = models.ForeignKey(A)                                 
    b = models.IntegerField()                                 

class C(models.Model):                                        
    fc = models.ForeignKey(B)                                 
    c = models.IntegerField()                                 

class BC(models.Model):                                       
    fc = models.ForeignKey(A)                                 
    fb = models.ForeignKey(B)                                 

And instead of having InlineB in Admin of model A, use inline of BC. So full admin.py looks like.

class InlineC(admin.TabularInline):
    model = C
    extra = 1

class BCInline(admin.TabularInline):
    model = BC
    extra = 1

class AdminA(admin.ModelAdmin):
    fieldsets = [
        (None, {
            'fields': ('a',)
            }),
        ]
    inlines = [BCInline]

class AdminB(admin.ModelAdmin):
    fieldsets = [
        (None, {
            'fields': ('b',)
            }),
        ]
    inlines = [InlineC]

And voila, I get button for popus to create full object of B, in the add page of Model A.

Neo
  • 13,179
  • 18
  • 55
  • 80