Automatically create OneToOne related object

08 Oct · by Tim Kamanin · 1 min read

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)

Comments

Required for comment verification