0

I have some issue. I import django User model, create new user then trying to get it profile, all what I have is "Profile matching query does not exist". Why? I just create the user.

Here is my code:

from django.contrib.auth.models import User 
user = User.objects.create(username="stackoverflow", password="tester1234")
user.get_profile()
the
  • 21,007
  • 11
  • 68
  • 101
James
  • 13,571
  • 6
  • 61
  • 83

4 Answers4

2

You might have forgotten to set

AUTH_PROFILE_MODULE

in your settings.py.

Gottlieb Notschnabel
  • 9,408
  • 18
  • 74
  • 116
Mihai Oprea
  • 2,051
  • 3
  • 21
  • 39
1

The online documentation suggests that get_profile()...

Returns a site-specific profile for this user. Raises django.contrib.auth.models.SiteProfileNotAvailable if the current site doesn't allow profiles, or django.core.exceptions.ObjectDoesNotExist if the user does not have a profile. For information on how to define a site-specific user profile, see the section on storing additional user information below.

Are you sure you've enabled profiles?

From your code snippet it looks like you've perhaps not created a profile which is a separate class (see here.

Community
  • 1
  • 1
Jon Cage
  • 36,366
  • 38
  • 137
  • 215
1

also write save method in signals.py:

@receiver(post_save,sender=User)
def save_profile(sender,instance,**kwargs):
    instance.profile.save()

and also add this in app.py

class UserProfileConfig(AppConfig):
    name = 'UserProfile'

    def ready(self):
        import UserProfile.signals
ilyankou
  • 1,309
  • 8
  • 13
Ashok
  • 11
  • 1
0

Django docs define this clearly, I miss that, sorry

Storing additional information about users

If you'd like to store additional information related to your users, Django provides a method to specify a site-specific related model -- termed a "user profile" -- for this purpose.

To make use of this feature, define a model with fields for the additional information you'd like to store, or additional methods you'd like to have available, and also add a OneToOneField named user from your model to the User model. This will ensure only one instance of your model can be created for each User. For example:

from django.contrib.auth.models import User

class UserProfile(models.Model):
    # This field is required.
    user = models.OneToOneField(User)

    # Other fields here
    accepted_eula = models.BooleanField()
    favorite_animal = models.CharField(max_length=20, default="Dragons.")

To indicate that this model is the user profile model for a given site, fill in the setting AUTH_PROFILE_MODULE with a string consisting of the following items, separated by a dot:

The name of the application (case sensitive) in which the user profile model is defined (in other words, the name which was passed to manage.py startapp to create the application). The name of the model (not case sensitive) class.

For example, if the profile model was a class named UserProfile and was defined inside an application named accounts, the appropriate setting would be:

AUTH_PROFILE_MODULE = 'accounts.UserProfile'

When a user profile model has been defined and specified in this manner, each User object will have a method -- get_profile() -- which returns the instance of the user profile model associated with that User.

The method get_profile() does not create a profile if one does not exist. You need to register a handler for the User model's django.db.models.signals.post_save signal and, in the handler, if created is True, create the associated user profile:

in models.py

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

# definition of UserProfile from above
# ...

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

post_save.connect(create_user_profile, sender=User)
James
  • 13,571
  • 6
  • 61
  • 83