Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make and enforce a generic OneToOne relation in django?

I'd like the exact same thing as django.contrib.contenttypes.generic.GenericForeignKey, but OneToOne instead of ForeignKey. I thought an easy (albeit marginally inelegant) workaround was to add unique=True to the field in question, but that borks.

like image 483
jMyles Avatar asked Feb 04 '11 01:02

jMyles


1 Answers

Use unique_together?

Any combination of the content_type field and the ID field is a unique identifier for one object, therefore 1 to 1.

http://docs.djangoproject.com/en/dev/ref/models/options/#unique-together

like image 153
Yuji 'Tomita' Tomita Avatar answered Oct 21 '22 11:10

Yuji 'Tomita' Tomita