as per title.
+8
A:
Set the related_name attribute.
If object Egg has a ManyToManyField pointing to Spam, and you set the related name to egg_set
, you can access the eggs via Spam.egg_set.all()
(or use filter()
to get a specific egg as shown below).
So to check if the Spam object my_spam
has the Egg with the ID 42, you could do something like:
if my_spam.egg_set.filter(pk=42):
fry_bacon()
mikl
2009-06-06 20:42:19