Automatically create OneToOne related object

Oct 08, 2013 · by Tim Kamanin

If you have a parent model that has OneToOne relation to some other model and you want this child model to be created upon parent model creation you need to install django-annoying package like this:

pip install django-annoying

enable annoying app and replace your models.OneToOneField with AutoOneToOneField like this:

class MyProfile(models.Model):
  user = AutoOneToOneField(User, primary_key=True)
  home_page = models.URLField(max_length=255, blank=True)
  icq = models.IntegerField(max_length=255, null=True)

Want to get more 🔥 tips like this one?

Subscribe to get notified about new dev tutorials