3

I'm trying to add fields to the User model and add them to the admin page. There is a recommended method in the django docs here:

https://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users

So, I created a OneToOne field for my new model:

from django.contrib.auth.models import User

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    designs = models.ManyToManyField('Design', blank=True)
    prints = models.ManyToManyField('Print', blank=True)
    rating = models.IntegerField(null=True, blank=True)
    reliability = models.IntegerField(null=True, blank=True)
    av_lead_time = models.IntegerField(null=True, blank=True)

Added an AUTH_PROFILE_MODULE to settings.py:

AUTH_PROFILE_MODULE = 'website.UserProfile'

Tried to add the UserProfile fields to the admin page:

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from website.models import UserProfile
from django.contrib.auth.models import User

# Define an inline admin descriptor for UserProfile model
# which acts a bit like a singleton
class UserProfileInline(admin.StackedInline):
    model = UserProfile
    can_delete = False
    verbose_name_plural = 'profile'

# Define a new User admin
class UserAdmin(UserAdmin):
    inlines = (UserProfileInline, )

# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, UserAdmin)

Now, when I try to access a registered user via the admin menu, I get:

Caught DoesNotExist while rendering: User matching query does not exist.
In template /usr/local/lib/python2.7/dist-packages/django/contrib/admin/templates/admin/includes/fieldset.html, error at line 19
19            {{ field.field }}

And when I try to add a new user via the admin menu, I get:

Caught DoesNotExist while rendering: User matching query does not exist.
In template /usr/local/lib/python2.7/dist-packages/django/contrib/admin/templates/admin/includes/fieldset.html, error at line 19
19            {{ field.field }}

Why doesn't it recognise that particular field?

3
  • Does you create UserProfile entry for existing users? Commented Oct 19, 2012 at 10:05
  • I don't quite know what you mean. I want to be able to create the new fields (UserProfile) for existing users as well as new users. However, I only have two existing users (admin and a test user). Commented Oct 19, 2012 at 10:16
  • Try flip-flopping the inlines. Unregister the django.user if you do not want it there, register Userprofile and add an inline for User. Commented Oct 20, 2012 at 1:20

3 Answers 3

2

Edit: After looking on the full error message I can see the error is not solely related to extending User. The error happens when rendering checkboxes and corresponding labels that are used to assign prints to UserProfile you are editing/adding. Django admin is calling Print.__unicode__ for rendering label for each Print instance, which in turn access (on line 33 of /threedee/website/models.py) the Print's "printer" attribute which is a foreign key to User. And for some reason one of the Prints does have invalid printer value which doesn't point to any User.

Can't really tell what is really happening here without seeing the Print model, I recommend you checking the Print database table (should be named website_print) and find if there is anything unusual (are you using PostgreSQL?). If you are not having any important data there, truncating whole Print table should do the trick.

This is my old answer which you should still follow but it's not related to the error you are experiencing:

I would just comment on others answers but there doesn't seem to be a way of doing that for me. You need to combine both Alexey Sidorov's and Like it's answers:

First use django shell to create UserProfile instances for existing users - just run commads provided by Like it's answer in the shell (python manage.py shell).

After that you should setup signal that will automatically create UserProfile for each new user according to answer from alexey-sidorov.

Sign up to request clarification or add additional context in comments.

4 Comments

You will be able to comment once you have enough "reputation points".
Thanks for that. I've just done those things and still get the same error message: dpaste.com/815515 - not sure what to try next.
Ok, I can see where is the problem probably, I've edited my aswer
Fantastic! I truncated the table website_print because there was only a bit of data there anyway. Thanks v. much.
1

Add this to models.py, after UserProfile class.

      from django.db.models.signals import post_save  
      def create_user_profile(sender, instance, created, **kwargs):
          if created:
              UserProfile.objects.create(user=instance)

      post_save.connect(create_user_profile, sender=User)

more info https://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users

3 Comments

I've added that now, but I still get the same error: Caught DoesNotExist while rendering: User matching query does not exist.
You should delete old user and add another one. So, just read the documentation.
I can't delete the user: dpaste.com/815484 and I can't add a new user - I get the same error as before. Also, I can't find in the documentation where it says this.
1

Add UserProfile entry for you existing users:

from django.contrib.auth.models import User
from website.models import UserProfile
for user in User.objects.all():
    profile = UserProfile.objects.get_or_create(user = user)

and create profile for new user as well. use signals for what.

1 Comment

Signals are not a good way to create UserProfile. Say if you have a datamigration in contrib.auth or you make the admin user with syncdb, the signal will break because the app that has the UserProfile is not synced. Best way is to have a custom auth backend that would create an empty user profile upon login or something. Even management command. But, avoid signals.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.