Django: Access Admin User in Python Shell

Nick B picture Nick B · Jul 12, 2013 · Viewed 18.8k times · Source

I am using Django Userena, but when trying to login to the django admin screen userena is creating a fuss: Profile matching query does not exist.

How can I create a profile for the admin user using the shell? Here is my Profile model, but I don't know how to access the key for the admin user to create a Profile object for it:

class Profile(UserenaBaseProfile, FacebookProfileModel):
    user = models.OneToOneField(User,
                                unique=True,
                                verbose_name=('user'),
                                related_name='my_profile')
    website = models.URLField(null=True, blank=True)

I know this is probably pretty simple, but google has failed me so far... Thanks for your ideas!

PS. I used to be able to login to my admin screen without issue, but something has changed now and I am getting the Profile matching query does not exist error (I didn't change the code, either).. Any ideas why I can't login now are also appreciated. Thanks!

Answer

karthikr picture karthikr · Jul 12, 2013

You can do something like this:

$ ./manage.py shell
> from django.contrib.auth.models import User
> from <profile_package>.models import Profile
> user = User.objects.get(username='<admin_user_name>')
> profile = Profile(user=user)
> profile.save()