Opened 5 years ago

Closed 5 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:

Description

Code example:

class MyModel(models.Model):
    @classmethod
    def get_current(self):
        self.objects.filter(pk=1).get()

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

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

To fix this behaviour I simply overwrite exception as this:

MyProxyModel.DoesNotExist = MyModel.DoesNotExist

Change History (6)

comment:1 Changed 5 years ago by vladimir_webdev

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Version changed from 1.2 to 1.3-alpha

comment:2 Changed 5 years ago by vladimir_webdev

  • Cc vladimir.webdev@… added
  • Component changed from Uncategorized to Database layer (models, ORM)

comment:3 Changed 5 years ago by mattmcc

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 5 years ago by rasca

  • Resolution set to worksforme
  • Status changed from new to closed

Neither do I... closing it as worksforme

comment:5 Changed 5 years ago by vladimir_webdev

  • Resolution worksforme deleted
  • Status changed from closed to reopened

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:

app/
    __init__.py
    package1/
        __init__.py
        models.py
    package2/
        __init__.py
        models.py

package1/models.py:

from django.db import models

class MyModel(models.Model):
    @classmethod
    def get_current(cls):
        MyModel.objects.get(pk=1000)

package2/models.py:

from app.package1 import models

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

view:

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

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

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

    return http.HttpResponse('ok')

comment:6 Changed 5 years ago by mattmcc

  • Resolution set to worksforme
  • Status changed from reopened to closed

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 irc.freenode.net or the django-users mailing list.

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