FK relations hell : help with `_set` feature

40 views
Skip to first unread message

François GUÉRIN

unread,
Mar 30, 2015, 8:04:00 AM3/30/15
to django...@googlegroups.com
Hi all,

I'm using django for a couple of years, and I've a question about relations in the ORM and templates. I massively use class-based generic views.

I' currently working with a model which have many other models FKing to it :


class People(models.Model):
    last_name = models.CharField(max_length=255)
    ... other fields ...

class PhoneNumber(models.Model):
    people = models.ForeignKey("my_app.People")
    number =
models.CharField(max_length=255)
    ... other fields ...

According to documentation, it is possible to use a `People.phonenumber
_set` attribute from a people instance, queryset'ing related phone numbers from people.

in may template "
myapp/people_detail.html", when I query this attribute from my template, by using {% for phonenumber in object.phonenumber_set.all %}[% endfor %}, I do not any phone number...

When I step -to-step debug the application, I can see a "myapp_phonenumber_related" related manager, but no
phonenumber_set.

Do I miss something ?

Thanks for your great job...


Daniel Roseman

unread,
Mar 30, 2015, 8:31:19 AM3/30/15
to django...@googlegroups.com
You don't seem to be giving all the information here. If you have a  `myapp_phonenumber_related` manager, that can only be because you've set that explicitly as the `related_name` attribute of the ForeignKey. And having done so, that's what you should be using in place of `phonenumber_set` anyway.
--
DR.

François GUÉRIN

unread,
Mar 30, 2015, 8:39:17 AM3/30/15
to django...@googlegroups.com

OK, it's working !

Thanks again !
FG.
Reply all
Reply to author
Forward
0 new messages