Nested Foreign Key relationships in Admin?

Visto 138 veces
Saltar al primer mensaje no leído

Dan

no leída,
15 dic 2006, 23:22:3915/12/06
a Django users
I'd like to represent foreign key relationships inside the Admin
interface when there are multiple nested one-to-many relationships.
Here's a simple example model to demonstrate, using the
edit_inline=models.TABULAR parameter:

__________

class Root(models.Model):
name = models.CharField(maxlength=200)
class Admin:
pass

class Model2(models.Model):
root = models.ForeignKey(Root, edit_inline=models.TABULAR,
num_in_admin=3)
description = models.CharField(maxlength=2048, core=True)

class Model3(models.Model):
model2 = models.ForeignKey(Model2, edit_inline=models.TABULAR,
num_in_admin=3)
description = models.CharField(maxlength=500, core=True)

__________

In the admin, when editing an instance of "Root", I can create/edit
associated "Model2" rows inline, but there is no inline interface or
link to create or view "Model3" rows. Is there a techinique available
to handle this? Strictly speaking, I don't necessarily need the nested
view to appear on the same page, as a link to the "Model3" view would
suffice.

I'm just getting my feet wet with Django, and I am very impressed!
Thanks for any possible insight.

Dan

Aditya Sriram M

no leída,
20 may 2012, 23:57:2520/5/12
a django...@googlegroups.com
I too have a similar issue. Any workarounds to achieve this...

Mike Dewhirst

no leída,
21 may 2012, 0:45:0521/5/12
a django...@googlegroups.com
On 21/05/2012 1:57pm, Aditya Sriram M wrote:
> I'd like to represent foreign key relationships inside the Admin
> interface when there are multiple nested one-to-many relationships.

Provided you have the admin app enabled, in your admin.py in the same
directory as your models.py file you need some nested classes ...

class RootAdmin(admin.ModelAdmin):
# any ModelAdmin options

class Model2Admin(admin.TabularInline): # or admin.StackedInline
# any TabularInline options

class Model3Admin(admin..TabularInline): # or
admin.StackedInline
# any TabularInline options

... which ought to give you a nested view of Model2 when you examine
Root in the admin and a nested view of Model3 inside Model2. You can use
whatever options are listed in the docs (see below) to control what
happens when displaying records in the Admin app..

https://docs.djangoproject.com/en/dev/ref/contrib/admin/#inlinemodeladmin-objects

Mike

Mike Dewhirst

no leída,
21 may 2012, 0:55:2821/5/12
a django...@googlegroups.com
On 21/05/2012 2:45pm, Mike Dewhirst wrote:
> On 21/05/2012 1:57pm, Aditya Sriram M wrote:
>> I'd like to represent foreign key relationships inside the Admin
>> interface when there are multiple nested one-to-many relationships.
>
> Provided you have the admin app enabled, in your admin.py in the same
> directory as your models.py file you need some nested classes ...
>
> class RootAdmin(admin.ModelAdmin):
> # any ModelAdmin options
>
> class Model2Admin(admin.TabularInline): # or admin.StackedInline
> # any TabularInline options
>
> class Model3Admin(admin..TabularInline): # or
> admin.StackedInline
> # any TabularInline options

Actually, I meant to name the nested classes more appropriately as
Model2Inline() and Model3Inline().

Then at the bottom of the admin.py file you need ...

admin.site.register(Root, RootAdmin)

... which includes the ModelxInlines because they are nested in your code.

Mike

Matt Schinckel

no leída,
21 may 2012, 5:26:5821/5/12
a django...@googlegroups.com
The admin.*Inline objects will not render nested inlines.


Matt.

Mike Dewhirst

no leída,
21 may 2012, 21:56:3221/5/12
a django...@googlegroups.com
On 21/05/2012 2:55pm, Mike Dewhirst wrote:
> On 21/05/2012 2:45pm, Mike Dewhirst wrote:
>> On 21/05/2012 1:57pm, Aditya Sriram M wrote:
>>> I'd like to represent foreign key relationships inside the Admin
>>> interface when there are multiple nested one-to-many relationships.
>>
>> Provided you have the admin app enabled, in your admin.py in the same
>> directory as your models.py file you need some nested classes ...
>>
>> class RootAdmin(admin.ModelAdmin):
>> # any ModelAdmin options
>>
>> class Model2Admin(admin.TabularInline): # or admin.StackedInline
>> # any TabularInline options
>>
>> class Model3Admin(admin..TabularInline): # or
>> admin.StackedInline
>> # any TabularInline options

As Matt pointed out the Model3 inline won't be nested. But you can
un-indent it to the same level as the Model2 inline and both will be
editable within the RootAdmin. I have done this previously and just
assumed it would work at an extra level. I'll try to avoid that in future.

Mike
Responder a todos
Responder al autor
Reenviar
0 mensajes nuevos