[Answered ]-Django admin – property options based on a related parent model

1👍

You can define which values are shown for a many-to-many field on the admin site with the formfield_for_manytomany() method.

Example for showing a union of the Parent’s read_users for Child objects:

class ChildAdmin(admin.ModelAdmin):
    def formfield_for_manytomany(self, db_field, request, **kwargs):
        if db_field.name == "read_users":
            kwargs["queryset"] = User.objects.filter(pk__in=(list(Parent.objects.values_list("read_users__pk", flat=True))))
        return super().formfield_for_manytomany(db_field, request, **kwargs)

See official Django documentation for more details. 

Leave a comment