Automatically create OneToOne related object

Oct 08, 2013 · Updated: Jul 12, 2021 · 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)

Hey, if you've found this useful, please share the post to help other folks find it:

There's even more:

Subscribe for updates

  • via Twitter: @timonweb
  • old school RSS:
  • or evergreen email ↓ ↓ ↓