MultipleObjectsReturned get() returned more than one User -- it returned 2!

Raised by:

django/db/models/query.py (line 361)

Traceback (most recent call last):
  File "python2.7/site-packages/django/core/handlers/base.py", line 111, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "python2.7/site-packages/django/views/generic/base.py", line 69, in view
    return self.dispatch(request, *args, **kwargs)
  File "python2.7/site-packages/django/views/generic/base.py", line 87, in dispatch
    return handler(request, *args, **kwargs)
  File "private.py", line 1, in private_function
    private_function()
  File "python2.7/site-packages/django/db/models/manager.py", line 92, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "python2.7/site-packages/django/db/models/query.py", line 361, in get
    num if num <= MAX_GET_RESULTS else 'more than %s' % MAX_GET_RESULTS
MultipleObjectsReturned: get() returned more than one User -- it returned 2!

How to reproduce

Add two users with the same value for some property and try to get by this property.

user1 = User.objects.create(height='1.70')
user2 = User.objects.create(height='1.70')

User.objects.get(height='1.70')

How to fix

If you expect duplicate instances in the database

  • Delete instances of the user until there's only one in the database OR
  • Change your query to:
User.objects.filter(height='1.70').first()

This will return only the first found instance of the user.

If you don't expect duplicate instances in the datebase

  • Delete instances of the user until there's only one in the database
  • Add the attribute unique=True to the desired User model field
  • Generate migrations