python - many-to-many in list display django -


class purchaseorder(models.model):     product = models.manytomanyfield('product')     vendor = models.foreignkey('vendorprofile')     dollar_amount = models.floatfield(verbose_name='price')   class product(models.model):    products = models.charfield(max_length=256)     def __unicode__(self):        return self.products 

i have code. unfortunately, error comes in admin.py "manytomanyfield"

class purchaseorderadmin(admin.modeladmin):     fields = ['product', 'dollar_amount']     list_display = ('product', 'vendor') 

the error says "'purchaseorderadmin.list_display[0]', 'product' manytomanyfield not supported."

however, compiles when take 'product' out of list_display. how can display 'product' in list_display without giving errors?

edit: maybe better question how display manytomanyfield in list_display?

you may not able directly. from documentation of list_display

manytomanyfield fields aren’t supported, because entail executing separate sql statement each row in table. if want nonetheless, give model custom method, , add method’s name list_display. (see below more on custom methods in list_display.)

you can this:

class purchaseorderadmin(admin.modeladmin):     fields = ['product', 'dollar_amount']     list_display = ('get_products', 'vendor')      def get_products(self, obj):         return "\n".join([p.products p in obj.product.all()]) 

or define model method, , use that

class purchaseorder(models.model):     product = models.manytomanyfield('product')     vendor = models.foreignkey('vendorprofile')     dollar_amount = models.floatfield(verbose_name='price')      def get_products(self):         return "\n".join([p.products p in self.product.all()]) 

and in admin list_display

list_display = ('get_products', 'vendor') 

Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -