python - Django ManyToMany Field with field name values -
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 want show name of category field in filter search , want save them objects while doing save.
how customise show name of manytomany field , on save save objects of manytomany field
add __unicode__
method model return string want use.
for python 3, use __str__
instead.
# on productsinfo model def __str__(self): return self.category.name
Comments
Post a Comment