Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- How to annotate all the objects with a new property?
- class Author(Model):
- state = models.CharField('ST')
- class Essay(Model):
- authors = models.ManyToManyField("Author",
- through='EssayAuthor')
- >> mylist
- [ <Essay: A1>, <Essay: B4>, <Essay: C9>, <Essay: A3> ... ]
- >> essay0 = mylist[0]
- >> essay0.state
- 'AK'
- class Essay(Model):
- authors = models.ManyToManyField("Author",
- through='EssayAuthor')
- @property
- def state(self):
- if self.authors.count():
- return self.authors[0].state
- else:
- return null
Add Comment
Please, Sign In to add comment