Search code examples
djangodjango-grappelli

django manytomany field with search option


I am trying to use custom search option in manytomany field for admin panel. It does not work my way. After search I want to show selected item as a list below the search field, currently it shows comma separated value in search field. Here my code

class MyModel(models.Model):
my_data= models.ManyToManyField(Topic, blank=True, null=True, related_name='mymodel_data')

admin.py

class MyModelCustom(admin.ModelAdmin):
model = MyModel
# define the raw_id_fields
raw_id_fields = ('my_data',)
# define the related_lookup_fields
related_lookup_fields = {
    'my_data': ['my_data']
}
admin.site.register(MyModel, MyModelCustom)

please help


Solution

  • the related_lookup_fields in your ModelAdmin is wrong. It needs to be:

    related_lookup_fields = {
        'm2m' : ['my_data']
    }