I have 2 models, Product and Tag. The relation between product and tag is Many-to-Many Relationship.
How to show "tags" field on django admin panel? Currently the value is None when I am using the code below
models.py
class Tag(models.Model):
name = models.CharField(max_length=200, null=True)
def __str__(self):
return self.name
class Product(models.Model):
CATEGORY = (
('Indoor','Indoor'),
('Outdoor','Outdoor'),
)
name = models.CharField(max_length=200, null=True)
price = models.FloatField(null=True)
category = models.CharField(max_length=200, choices=CATEGORY)
description = models.CharField(max_length=200, null=True)
date_created = models.DateTimeField(auto_now_add=True, null=True)
tags = models.ManyToManyField(Tag)
admin.py
@admin.register(Product)
class ProductAdmin(admin.ModelAdmin):
list_display = ['id','name','price','category','tags']
list_display_links = ['name']
def tags(self):
return self.tags.name
@admin.register(Product)
class ProductAdmin(admin.ModelAdmin):
list_display = ['id','name','price','category','get_tags']
list_display_links = ['name']
def get_tags(self, instance):
return [tag.name for tag in instance.tags.all()]