How can I access obj in admin.py

26 views
Skip to first unread message

Mike Dewhirst

unread,
May 3, 2017, 5:34:31 AM5/3/17
to Django users
I want to adjust inlines based on a property of the object.

class SubstanceAdmin(admin.ModelAdmin):

class SolidInline(admin.StackedInline):
pass
class LiquidInline(admin.StackedInline):
pass
class GasInline(admin.StackedInline):
pass

inlines = get_inlines

def get_inlines(self):
obj = how_do_I_get_this(?)
if obj.physical_state = None:
return ((SolidInline), (LiquidInline), (GasInline))
elif obj.physical_state = 1:
return ((SolidInline), )
elif obj.physical_state = 2:
return ((LiquidInline), )
elif obj.physical_state = 3:
return ((GasInline), )

Many thanks for a miracle :)

Mike

Stephen J. Butler

unread,
May 3, 2017, 7:13:20 AM5/3/17
to django...@googlegroups.com



Mike

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a138bd6c-46fb-98c7-ba28-69a5959e5fbc%40dewhirst.com.au.
For more options, visit https://groups.google.com/d/optout.

Mike Dewhirst

unread,
May 3, 2017, 10:58:03 PM5/3/17
to django...@googlegroups.com
On 3/05/2017 5:12 PM, Stephen J. Butler wrote:
> You want to override get_inline_instances:
> https://docs.djangoproject.com/en/1.11/ref/contrib/admin/#django.contrib.admin.ModelAdmin.get_inline_instances

Many thanks Stephen - works nicely.

And thanks again to all you Django devs :)

M
> send an email to django-users...@googlegroups.com
> <mailto:django-users%2Bunsu...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> <https://groups.google.com/group/django-users>.
> <https://groups.google.com/d/msgid/django-users/a138bd6c-46fb-98c7-ba28-69a5959e5fbc%40dewhirst.com.au>.
> For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAD4ANxWWNxfDSCGUm8GFR_ijGL2WZAC%2Bwz3NCwpA%3DyHyALfWZQ%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAD4ANxWWNxfDSCGUm8GFR_ijGL2WZAC%2Bwz3NCwpA%3DyHyALfWZQ%40mail.gmail.com?utm_medium=email&utm_source=footer>.
Reply all
Reply to author
Forward
0 new messages