Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making inlines conditional in the Django admin

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?

like image 297
Jason Goldstein Avatar asked Mar 23 '11 21:03

Jason Goldstein


People also ask

What is Inlines in Django admin?

The admin interface is also customizable in many ways. This post is going to focus on one such customization, something called inlines. When two Django models share a foreign key relation, inlines can be used to expose the related model on the parent model page. This can be extremely useful for many applications.

How do I change the admin style in Django?

Sometimes you can just extend the original admin file and then overwrite a block like {% block extrastyle %}{% endblock %} in django/contrib/admin/templates/admin/base. html as an example. If your style is model specific you can add additional styles via the Media meta class in your admin.py .

How do I get admin access to Django?

To login to the site, open the /admin URL (e.g. http://127.0.0.1:8000/admin ) and enter your new superuser userid and password credentials (you'll be redirected to the login page, and then back to the /admin URL after you've entered your details).


1 Answers

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 calls get_inline_instances, we can modify both functions to act on the object.

This should work:

class ThingAdmin(admin.ModelAdmin):
    model = Thing

    inlines = [inline]
    other_set_of_inlines = [other_inline]

    def get_inline_instances(self, request, obj=None):
        #                                    ^^^ this is new
        inline_instances = []

        if obj.date > datetime.date(2012, 1, 1):
            inlines = self.inlines
        else:
            inlines = self.other_set_of_inlines

        for inline_class in inlines:
            inline = inline_class(self.model, self.admin_site)
            if request:
                if not (inline.has_add_permission(request) or
                        inline.has_change_permission(request) or
                        inline.has_delete_permission(request)):
                    continue
                if not inline.has_add_permission(request):
                    inline.max_num = 0
            inline_instances.append(inline)
        return inline_instances

    def get_formsets(self, request, obj=None):
        for inline in self.get_inline_instances(request, obj):
            #                                           ^^^^^ this is new
            yield inline.get_formset(request, obj)
like image 72
Yuji 'Tomita' Tomita Avatar answered Sep 18 '22 15:09

Yuji 'Tomita' Tomita