On Sun, May 16, 2010 at 6:41 PM, Odagi <fcmira...@gmail.com> wrote:
> Hello users, I cannot realize the difference between the different
> ways of extending a model. Suppose we have Place and Restaurant Models
> (a Restaurant is an extension of a Place). I know 3 different ways of
> define the data model:
>
> 1. Multi-table inheritance:
>
> class Place(models.Model): ...
> class Restaurant(Place): ...

restaurant table gets all fields from place,
you can access all fields with single sql query

> 2. OneToOne relationship:
>
> class Place(models.Model): ...
> class Restaurant(models.Model):
>    place = models.OneToOneField(Place, primary_key=True)

You access "place" fields by using: restaurant.place
with extra db call unless you use select_related('place')

>
> 3. ForeignKey unique:
>
> class Place(models.Model): ...
> class Restaurant(models.Model):
>    place = models.ForeignKey(Place, unique=True)

In the old days there wasn't OneToOneField and people
used this method to have one-one relationship.
If you need to use ForeignKey+unique, better use OneToOneField.

One difference is the related name, with ForeignKey+unique
you would need to use place.restaurant_set[0] to access related
restaurant. Using OneToOneField just use place.restaurant.

~Rolando

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

Reply via email to