python - How to include a ManyToManyField in an email? Django -


i have code set send user email whenever new purchaseorder created in database. email looks this:

as can see product field blank. reason it's blank using manytomanyfield , not sure how email user. here's code it:

class purchaseorder(models.model):     product = models.manytomanyfield('product')  def send_email_on_new_order(instance, created, raw, **kwargs):     if not created or raw:         return      title_text =  'this title'     body_text = 'purchase order system details' + 'product: ' + 'vendor: ' + str(instance.vendor) + 'price: ' + str(instance.price)     email=emailmessage(title_text, body_text, to=['youremail@gmail.com'])     email.content_subtype = "html"     email.send() signals.post_save.connect(send_email_on_new_order, sender= purchaseorder, dispatch_uid = 'send_email_on_new_order')  class product(models.model):     products = models.charfield(max_length=256)     price_for_each_item = models.floatfield()      def __unicode__(self):         return self.products 

basically include in body_text of email is: manytomanyfield it'll send email user 'products' , 'price_for_each_item'

you'll need iterate through products.

i think can like:

for item in instance.products:     body_text += 'product': item 

although, should using template , emailmessage class.


Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

php - MySQLi multi_query results for later use -