Wednesday, March 6, 2013

Re: reverse foreign key relationship usage

Hi Fatih,

This is extremely useful in case you have two different relations
between the same model. As a rough example, consider the two following
models:

class Person(models.Model):
name = models.CharField(max_length=30)

class Car(models.Model):
model = models.CharField(max_length=20)
owner = models.OneToOne(Person, related_name='owned_car')
users = models.ForeignKey(Person, related_name='used_car')

In case of not having a different related_name for each of those two
relations, you wouldn't be able to get the person's car or the car this
person uses. (This person may own a car but use his company car, or a
friend's, a rental one...)





On 03/06/2013 08:30 AM, Fatih Tiryakioglu wrote:
> Hello all,
>
> In Appendix B:Database API, it says "related_name (keyword) is
> particularly useful if a model has two foreign keys to the same second
> model." which is about 'related_name' keyword used reverse foreign key
> relationships.
> e.g. blog = ForeignKey(Blog, related_name='entries').
>
> What does it mean? Where should I use related_name instead of simply
> reversing by "x_set" keyword.
>
> Thanks.
>
>
> --
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users+unsubscribe@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>


--
Kind regards,

Roberto López López


System Developer
Parallab, Uni Computing
Høyteknologisenteret, Thormøhlensgate 55
N-5008 Bergen, Norway
Tel: (+47) 555 84091

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment