django admin: How to customize one field in fieldsets?

Shang Wang picture Shang Wang · Mar 5, 2013 · Viewed 13k times · Source

I try to make one field in django admin's fieldsets to show only certain data, but according to django document, only an example of list_display is shown to be able to customize. I tried the similar approach on fieldsets like the following:

In models.py:

def ports_with_same_scanner(self):
    return PortList.objects.filter(scanner=self.scanner)
ports_with_same_scanner.short_description = 'port_lists'

In admin.py, this won't work:

fieldsets = (
            ('Scan Template', { 
            'fields': ( ('name', 'scanner', 'ports_with_same_scanner',), 'comment', ('in_use',
                'fc_growing', 'nc_growing'), 'nvt_prefs')
            }),
)

However, if I do this:

list_display = ('name', 'scanner', 'ports_with_same_scanner', 'comment', 'in_use', 'fc_growing', 'nc_growing', 'nvt_prefs')

the ports_with_same_scanner works just fine. The problem is that I don't want to change my display from fieldsets to list_display, I wonder how can I achieve the same functionality. Thanks.

Answer

Aidan Ewen picture Aidan Ewen · Mar 5, 2013

Would it work to apply the filter at the model level? If so you can use the limit_choices_to attribute on the model's ForeignKeyField.

Alternatively you could override the formfield_for_foreignkey attribute of the modelAdmin class.

Something like -

class YourModelAdmin(admin.ModelAdmin):
    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if db_field.name == "ports_with_same_scanner":
            kwargs["queryset"] = PortList.objects.filter(scanner=self.scanner)
        return super(YourModelAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)

(Apologies if I've misunderstood the question)