python - Displaying multiple instances in a ManyToManyField in Django -
here's have in image:
i display colored in green. display each product cost. bob cost $12010 , jill cost $12010 total price of $24020.
here's code far:
class purchaseorder(models.model): product = models.manytomanyfield('product', null =true) def get_products(self): return "<br> </br>".join([p.products p in self.product.all()]) get_products.allow_tags = true class product(models.model): products = models.charfield(max_length=256, null =true) price_for_each_item = models.floatfield() #here want displayed green
how can this? notice each product gets new line. i'm trying each indivdual item on same line indivdual item's price. example here is,
bob $12010.0
jill $12010.0
you can use string concatenation concatenate name price. if products
should first part of string , price_for_each_item
second part, use:
def get_products(self): return "<br />".join("%s %s" % (p.products, p.price_for_each_item) p in self.product.all())
a few unrelated comments:
note that, according xhtml standards (and recommended html standard), tag without real content, <br>
tag should self-closing (thus <br />
instead of <br> </br>
).
also, removing square brackets inside join function create generator instead of list. not create (useless) intermediary list in memory , process faster. think it's habit develop in case ever have work large lists reduced memory needs substantial.
and note, null=true
doesn't on manytomanyfield
. null=true
removes not null
clause on database-level, m2m relations saved in separate table entirely. if model has no related models in m2m relation, there no records in m2m table.
Comments
Post a Comment