views:

51

answers:

2

Both seems to be generating integer NOT NULL UNIQUE REFERENCES databaase columns.

Edit: My question is only about at the database level. (Not in the Django ORM.)

+1  A: 

Your answer is on the official documentation.

Basically, the difference is that when you try to access the ForeignKey from your object, you'll get another object, and not a queryset as you would in the ForeignKey.

From the docs:

class OneToOneField(othermodel[, parent_link=False, **options])

A one-to-one relationship. Conceptually, this is similar to a ForeignKey with unique=True, but the "reverse" side of the relation will directly return a single object.

Flávio Amieiro
Flavio: Thanks for the link. However my question is is there **any** difference `On a database level`.
uswaretech
@uswaretech as far as the documentation goes, there is no difference in the database level.
Flávio Amieiro
+1  A: 

ForeignKey fields should be used for 1 to n relationships and OneToOneField should be used to 1 to 1 relationships.

On database level, the foreign key is unique for OneToOneFields and that's not the case for ForeignKeys.

jbochi
With `ForeignKey(Model, unique = True)` the FK obviously is unique, so are you saying that there is no diff?
uswaretech
I'm sorry, I had not noticed that on question's title. I think there's no difference indeed.
jbochi