Search code examples
pythondjangodjango-syncdb

Django post_save hook causing an error for syncdb


I have a custom model with a OneToOne relationship with the User (django.contrib.auth.models.User) model;

class Profile(models.Model):
    user = models.OneToOneField(User)

And I want to create a new Profile every time a User is created. So I've added a signal;

@receiver(post_save, sender=User)
def _setup_user(sender, **kwargs):
    """
    Create a new profile instance for each user
    """
    created = kwargs.get('created', False)
    user = kwargs.get('instance', False)
    if created and user:
        profile = Profile(user=kwargs['instance'])
        profile.save()

All this is fine, however syncdb throws an error when creating the admin user because the Profile table has not been created yet (I'm using South to manage migrations).

How can I get around this?

EDIT

So, omitting creating a superuser worked;

$ ./manage.py syncdb --noinput
$ ./manage.py migrate
$ ./manage.py createsuperuser

Solution

  • Run syncdb first and decline making a superuser. Then after creating the db run manage.py createsuperuser

    Here's the docs for 1.6

    If you have anymore trouble let me know, but if the only issue is that the db isn't created first, then this should work.

    Hope it helps

    Cheers