code_junkie

How can I write a method within a Django model to retrieve related objects?

Nov 11th, 2011
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.47 KB | None | 0 0
  1. class Foo(models.Model):
  2. name = models.CharField()
  3.  
  4. class Bar(models.Model):
  5. title = models.CharField()
  6. Foo= models.ForeignKey('myapp.Foo')
  7.  
  8. class Foo(models.Model):
  9. name = models.CharField()
  10. def returnBars(self):
  11. ????
  12.  
  13. some_foo.bar_set.all()
  14.  
  15. class Foo(models.Model):
  16. name = models.CharField()
  17.  
  18. class Bar(models.Model):
  19. title = models.CharField()
  20. foo = models.ForeignKey(Foo, related_name='bars')
  21.  
  22. ...
  23.  
  24. some_foo.bars.all()
Add Comment
Please, Sign In to add comment