Guest User

Untitled

a guest
Aug 20th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. How to annotate all the objects with a new property?
  2. class Author(Model):
  3. state = models.CharField('ST')
  4.  
  5. class Essay(Model):
  6. authors = models.ManyToManyField("Author",
  7. through='EssayAuthor')
  8.  
  9. >> mylist
  10. [ <Essay: A1>, <Essay: B4>, <Essay: C9>, <Essay: A3> ... ]
  11.  
  12. >> essay0 = mylist[0]
  13. >> essay0.state
  14. 'AK'
  15.  
  16. class Essay(Model):
  17. authors = models.ManyToManyField("Author",
  18. through='EssayAuthor')
  19.  
  20. @property
  21. def state(self):
  22. if self.authors.count():
  23. return self.authors[0].state
  24. else:
  25. return null
Add Comment
Please, Sign In to add comment