How do I search in django admin-panel using tag?
It will search using tag which known as django-taggit package.
admin.py
@staticmethod
def get_tags(obj):
tags = []
for tag in obj.tags.all():
tags.append(str(tag))
return ', '.join(tags)
list_display = (
'id'
'portions',
'get_tags',
'created'
)
search_fields = ('name', get_tags, 'created', 'id')`
models.py
from taggit.managers import TaggableManager
tags = TaggableManager(through=TaggedOffer, blank=True)
name = models.CharField(max_length=255)
price = models.DecimalField(
null=True,
max_digits=10,
decimal_places=2,
validators=[positive_decimal]
)
Error
Exception Type: FieldError
Exception Value:
Cannot resolve keyword 'get_tags' into field.
in admin,py
search_fields = ('name', get_tags, 'created', 'id')change it to
search_fields = ('name', 'tags__name', 'created', 'id')