49👍
There is a formfield_for_manytomany. Usage is similar to the answer given by defuz.
ModelAdmin.formfield_for_manytomany(db_field, request, **kwargs)¶
Like the
formfield_for_foreignkey
method, theformfield_for_manytomany
method can be overridden to change the
default formfield for a many to many field. For example, if an owner
can own multiple cars and cars can belong to multiple owners – a many
to many relationship – you could filter the Car foreign key field to
only display the cars owned by the User:class MyModelAdmin(admin.ModelAdmin): def formfield_for_manytomany(self, db_field, request, **kwargs): if db_field.name == "cars": kwargs["queryset"] = Car.objects.filter(owner=request.user) return super(MyModelAdmin, self).formfield_for_manytomany(db_field, request, **kwargs)
31👍
Read the docs:
class BucketAdmin(admin.ModelAdmin):
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == "things":
kwargs["queryset"] = Things.objects.filter(...)
return super(BucketAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
- [Django]-How to use refresh token to obtain new access token on django-oauth-toolkit?
- [Django]-NameError: name '_mysql' is not defined after setting change to mysql
- [Django]-How to do a multi-step form in Django?
- [Django]-Raise a validation error in a model's save method in Django
- [Django]-Vscode html autoformat on django template
- [Django]-Django middleware difference between process_request and process_view
Source:stackexchange.com