Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't Create Super User Django

Tags:

django

I'm assuming that it is because my superuser depends on UserProfile which has no existing data yet. My model looks like

from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save

class UserProfile(models.Model):
    user = models.OneToOneField(User) # required
    location = models.CharField(max_length=100)
    age = models.PositiveIntegerField(blank=True,null=True)
    contribution_points = models.PositiveIntegerField()
    #acheivements = models.ManyToMany()

def create_user_profile(sender,instance,created,**kwargs):
    if created:
        UserProfile.objects.create(user=instance)

post_save.connect(create_user_profile, sender=User)

However, I end up with the following error:

django.db.utils.DatabaseError: (1146, "Table 'savory_db.login_userprofile' doesn't exist")

despite having just ran syncdb

Does my model have any contradictory fields that would cause this error. Should UserProfile not be applied to the superuser? How should I prevent this?

like image 940
user1431282 Avatar asked Dec 27 '12 18:12

user1431282


People also ask

Does Django have superuser permissions?

A Django superuser, is its name implies, means it's a user with 'super' permissions. By extension, this means a superuser has access to any page in the Django admin, as well as permissions to Create, Read, Update and Delete any type of model record available in the Django admin.

Which command is used to create a superuser in Django?

Type python3 manage.py createsuperuser in the given terminal and press “Enter”. The system will ask for credentials, after which a superuser will be created. To run the server, we type the command python3 manage.py runserver 0.0. 0.0:8000 and press “Enter”.

How many superuser can be created in Django?

Note that this won't prevent from setting a user as being a superuser from django admin interface. If you want to completely make it impossible to create two superusers, you can add the constraint on the database level directly.


1 Answers

On Mar 23, 2011, at 4:25 AM, Malcolm Box wrote:

Further investigation: looks like it's a South/syncdb interaction. The UserProfile will be created by the south migration, but of course that hasn't run when the auth post_install runs to prompt for a superuser.

Sadly syncdb --migrate doesn't do the right thing either.

For now, I'm just creating a superuser manually using ./manage.py shell, but would welcome any ideas on how to solve this better.

Don't create the super user during syncdb, you user profile table will not exist. You must have a create signal on admin that creates a user profile, this looks like it is failing

The procedure you wan to use to initialize the database is:

python manage.py syncdb --noinput
python manage.py migrate
python manage.py createsuperuser

Reference : https://groups.google.com/forum/?fromgroups=#!topic/django-users/sBXllxrIdMc

like image 141
user1431282 Avatar answered Sep 18 '22 15:09

user1431282