Opened 8 years ago

Closed 8 years ago

#14687 closed (worksforme)

Proxy model inheritance and DoesNotExist exception

Reported by: vladimir_webdev Owned by: nobody
Component: Database layer (models, ORM) Version: 1.3-alpha
Severity: Keywords: proxy model, DoesNotExist
Cc: vladimir.webdev@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


Code example:

class MyModel(models.Model):
    def get_current(self):

class MyProxyModel(MyModel):
    class Meta:
        proxy = True

    current = MyProxyModel,get_current()
except MyProxyModel.DoesNotExist: # will never be caught, because MyModel.DoesNotExist is actually raised

To fix this behaviour I simply overwrite exception as this:

MyProxyModel.DoesNotExist = MyModel.DoesNotExist

Change History (6)

comment:1 Changed 8 years ago by vladimir_webdev

Version: 1.21.3-alpha

comment:2 Changed 8 years ago by vladimir_webdev

Cc: vladimir.webdev@… added
Component: UncategorizedDatabase layer (models, ORM)

comment:3 Changed 8 years ago by Matt McClanahan

I can't reproduce this in 1.2 or trunk.

>>> from tst.models import MyProxyModel
>>> MyProxyModel.objects.all()
>>> try:
...     MyProxyModel.get_current()
... except MyProxyModel.DoesNotExist:
...     pass

comment:4 Changed 8 years ago by rasca

Resolution: worksforme
Status: newclosed

Neither do I... closing it as worksforme

comment:5 Changed 8 years ago by vladimir_webdev

Resolution: worksforme
Status: closedreopened

Sorry, I did not test code, because I thought it does not work in all situations. I got this bug if models are in different packages. My structure:



from django.db import models

class MyModel(models.Model):
    def get_current(cls):


from app.package1 import models

class MyModel(models.MyModel):
    class Meta:
        proxy = True


from app.package1 import models as package1_models
from app.package2 import models as package2_models

def test(request):
        current = package1_models.MyModel.get_current()
    except package1_models.MyModel.DoesNotExist: # ok, exception is caught

        current = package2_models.MyModel.get_current()
    except package2_models.MyModel.DoesNotExist: # can't caught

    return http.HttpResponse('ok')

comment:6 Changed 8 years ago by Matt McClanahan

Resolution: worksforme
Status: reopenedclosed

I'm afraid I still don't see the problem (other than get_current should call cls.objects.get). It's possible something else in your code is involved. I suggest getting someone to do some further investigating with you, such as on the #django channel on or the django-users mailing list.

Note: See TracTickets for help on using tickets.
Back to Top