Home > Net >  MultipleObjectsReturned() in Django error
MultipleObjectsReturned() in Django error

Time:07-08

I have posted the code below, when I try to view this model as an object in http://127.0.0.1:8000/admin/ I get the following error:

  • MultipleObjectsReturned at /admin/configuration/epemployeeposition/2/change/
    get() returned more than one Epemployeeposition -- it returned more than 20!

Does anyone know how to fix this? I would greatly appreciate the help!

This is my model of the object giving me an error in models.py. To create this model I autogenerated it from the postgres database using python manage.py inspectdb. I have only removed manage = False in the Meta class

postgres database:

Postgres Database View

models.py:

class Epemployeeposition(models.Model):
    companyid = models.IntegerField(db_column='CompanyID', primary_key=True)  
    employeeid = models.IntegerField(db_column='EmployeeID')  
    linenbr = models.IntegerField(db_column='LineNbr')  
    isactive = models.BooleanField(db_column='IsActive')  
    positionid = models.CharField(db_column='PositionID', max_length=20, blank=True, null=True)  
    startdate = models.DateTimeField(db_column='StartDate', blank=True, null=True)  
    startreason = models.CharField(db_column='StartReason', max_length=3)  
    enddate = models.DateTimeField(db_column='EndDate', blank=True, null=True)  
    isterminated = models.BooleanField(db_column='IsTerminated')  
    termreason = models.CharField(db_column='TermReason', max_length=3, blank=True, null=True)  
    isrehirable = models.BooleanField(db_column='IsRehirable')  
    noteid = models.UUIDField(db_column='NoteID')  
    createdbyid = models.UUIDField(db_column='CreatedByID')  
    createdbyscreenid = models.CharField(db_column='CreatedByScreenID', max_length=8)  
    createddatetime = models.DateTimeField(db_column='CreatedDateTime')  
    lastmodifiedbyid = models.UUIDField(db_column='LastModifiedByID')  
    lastmodifiedbyscreenid = models.CharField(db_column='LastModifiedByScreenID', max_length=8)  
    lastmodifieddatetime = models.DateTimeField(db_column='LastModifiedDateTime')  
    tstamp = models.BinaryField()

    class Meta:
        db_table = 'EPEmployeePosition'
        unique_together = (('companyid', 'employeeid', 'linenbr'), ('noteid', 'companyid'),)

Here is the full Traceback of the error

Traceback:

Traceback (most recent call last):
  File "/home/descartes/source/bitserf/env/lib/python3.8/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
  File "/home/descartes/source/bitserf/env/lib/python3.8/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/descartes/source/bitserf/env/lib/python3.8/site-packages/django/contrib/admin/options.py", line 683, in wrapper
    return self.admin_site.admin_view(view)(*args, **kwargs)
  File "/home/descartes/source/bitserf/env/lib/python3.8/site-packages/django/utils/decorators.py", line 133, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/home/descartes/source/bitserf/env/lib/python3.8/site-packages/django/views/decorators/cache.py", line 62, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "/home/descartes/source/bitserf/env/lib/python3.8/site-packages/django/contrib/admin/sites.py", line 242, in inner
    return view(request, *args, **kwargs)
  File "/home/descartes/source/bitserf/env/lib/python3.8/site-packages/django/contrib/admin/options.py", line 1888, in change_view
    return self.changeform_view(request, object_id, form_url, extra_context)
  File "/home/descartes/source/bitserf/env/lib/python3.8/site-packages/django/utils/decorators.py", line 46, in _wrapper
    return bound_method(*args, **kwargs)
  File "/home/descartes/source/bitserf/env/lib/python3.8/site-packages/django/utils/decorators.py", line 133, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/home/descartes/source/bitserf/env/lib/python3.8/site-packages/django/contrib/admin/options.py", line 1745, in changeform_view
    return self._changeform_view(request, object_id, form_url, extra_context)
  File "/home/descartes/source/bitserf/env/lib/python3.8/site-packages/django/contrib/admin/options.py", line 1768, in _changeform_view
    obj = self.get_object(request, unquote(object_id), to_field)
  File "/home/descartes/source/bitserf/env/lib/python3.8/site-packages/django/contrib/admin/options.py", line 861, in get_object
    return queryset.get(**{field.name: object_id})
  File "/home/descartes/source/bitserf/env/lib/python3.8/site-packages/django/db/models/query.py", line 499, in get
    raise self.model.MultipleObjectsReturned(
configuration.models.Epemployeeposition.MultipleObjectsReturned: get() returned more than one Epemployeeposition -- it returned more than 20!

Here is what it looks like in the admin panel

admin object error location: Epemployeeposition object(2)

The error when clicking on the Epemployeeposition object(2)

This is the error message received when trying to access the object

CodePudding user response:

I think this happens because there are repeated values with objects ...

  • Try to delete the data manage flush and recreate it and then make sure that your primary_key field has unique values
  • Or remove the primary_key from the first filed and Django will create id column this also will solve the problem

CodePudding user response:

i can see the problem is not with get , you are creating multiple objects for the same record. try to delete the entire objects and try creating a new one.

  • Related