DjangoDjango

Automatically create OneToOne related object

Oct 08, 2013 · Updated: Nov 10, 2017 · 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:

bash
pip install django-annoying

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

python
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 ↓ ↓ ↓