Django ManyToMany field with field names

ManyToMany Field Coming as object

model.py

class MedtechProductCategory(models.Model): name = models.CharField(max_length=128, null=False, blank=False) type = models.CharField(choices=type_choices_for_tag, max_length=512) class Meta: db_table = 'medtech_product_category' class ProductsInfo(models.Model): deal_active = models.BooleanField(default=True) category = models.ManyToManyField(MedtechProductCategory, related_name='product_info_category') class Meta: db_table = 'products_info' def getTags(self): return self.category.values_list() 

admin.py

 class ProductsInfoAdmin(admin.ModelAdmin): filter_horizontal = ('category',) admin.site.register(ProductsInfo, ProductsInfoAdmin) 

So, I want to show the name of the category field in the filter search and save them as objects when saving.

How to configure it to show the name of the manytomany field and save the saved objects of the manytomany field

+5
source share
1 answer

Add the __unicode__ method to your model, which will return the string you want to use.

For python 3, use __str__ instead.

 # on ProductsInfo model def __str__(self): return self.category.name 
+5
source

All Articles