I have a model that I want staff to be able to edit up to the date for the event. Like this:
class ThingAdmin(admin.ModelAdmin):
model = Thing
if obj.date < today: #Something like that
inlines = [MyInline,]
The problem is, I don't have access to the obj instance at this level. I've tried overriding get_formset(), but didn't get anywhere.
Please advise?
I had a complex case where the solutions I tried failed in unexpected ways (problems with readonly fields in inlines). This is the most clear and failsafe way I've found:
This is working in Django 1.4.x.
In recent version of Django, you'll need to override ModelAdmin.get_formsets. e.g.
Thanks to the comments for a change in 1.4. My implementation here wasn't thread safe either, so it really should have been deleted.
Since
get_formsets
is passed the object and callsget_inline_instances
, we can modify both functions to act on the object.This should work:
I had a situation where I needed to show an Inline based on the admin site that you were on for a given story.
I was able to get dynamic inlines working for Django 1.3 using the following code:
In highlights/admin.py
In story/admin.py
One thing to note is that I'm not merely manipulating inline classes(HighlightInline) but rather, I'm changing inline instances(HighlightInline(self.model, self.admin_site)). This is because django has already constructed a list of inline instances based on a list of inline classes during the initial construction of the admin class.
I think the best answer is in the django documentation: https://docs.djangoproject.com/en/1.9/ref/contrib/admin/
Search for "get_inline_instances" The example provided is very good and the nuances of the call are described in detail.
I think the easiest way to hack this is to call your custom funciton in
get_fields
, orget_fieldsets
and so on, just setself.inlines
in a custom function.