Foreign key with related name is not working properly.
|Reported by:||davisito89@…||Owned by:||nobody|
|Component:||Database layer (models, ORM)||Version:||1.3|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
I created a class Person and a class Email. The relationship is 1:n (a person can have many emails and an email belongs to only one person). My class Email contains an attribute named person = models.ForeignKey(u"Person", related_name = u"emails"). Then I get some errors trying to get the emails of a single person:
- I created a person.
Person.objects.create(id = 1)
- I created two emails.
Email.objects.create(email = u"bla1", person = Person.objects.get(pk = 1))
Email.objects.create(email = u"bla2", person = Person.objects.get(pk = 1))
- Try to get the emails of a person:
Person.objects.all().emails.all().email (prints "bla2", instead of "bla1")
Person.objects.all().emails.all().email (prints "bla2")
[x.name for x in Person.objects.all().emails.all()] prints "bla1" and "bla2" (which is the expected result).