Foreingkey - QuerySet value for an exact lookup must be limited to

I’ve implemented the following model that extends the information from Django’s generic User model.

class user_data (models.Model):

    user_id = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
    client_type= models.IntegerField(default=1, blank=True) 
    name = models.CharField(max_length=200, blank=True)
    last_name = models.CharField(max_length=200, blank=True)
    picture=models.ImageField(upload_to='media/', blank=True)	

But when I want to store data in it, I get the following console output

'The QuerySet value for an exact lookup must be limited to '
ValueError: The QuerySet value for an exact lookup must be limited to one result using slicing.
 [04/Jan/2023 00:30:28] "POST /completeinfo HTTP/1.1" 500 134219

Traceback (most recent call last):
  File "/home/julian/.local/lib/python3.6/site-packages/django/core/handlers/", line 47, in inner
    response = get_response(request)
  File "/home/julian/.local/lib/python3.6/site-packages/django/core/handlers/", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/julian/.local/lib/python3.6/site-packages/django/views/decorators/", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/home/julian/xxxxxxx/xxxxxx/xxxxxxx/xxxxxx/xxxxxxxxx/", line 217, in completeInfo
    userData =user_data.objects.filter(user_id=user).first()
  File "/home/julian/.local/lib/python3.6/site-packages/django/db/models/", line 674, in first
    for obj in (self if self.ordered else self.order_by('pk'))[:1]:

this is the method:

  def completeInfo (request): 
      if request.method == 'POST':
          if user: 
              userData =user_data.objects.filter(user_id=user).first()
              if userData:
                  userData.picture= request.FILES.get("image") 

The basic problem is that you’ve got the relationship defined as a many-to-one where each User could have many user_data related to it. What you’re likely looking for is a one-to-one relationship between the two models. Review the docs at Customizing authentication in Django | Django documentation | Django.

Side note: I would encourage you to adopt Python and Django’s naming conventions for classes. This means your profile class would more appropriately be named UserData.

Side note #2: Once you have the proper relationship defined between the two models, it’s no longer necessary to write a query to access data associated with the related object. The ORM provides direct bidirectional access between objects related by a one-to-one relationship.

1 Like

Thanks for the reply. Is it possible to have more than one model with a one-to-one relationship with the User model?

Absolutely. What leads you to believe that there can’t?

1 Like