Opened 14 years ago
Closed 14 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: | no | UI/UX: | no |
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 by , 14 years ago
Version: | 1.2 → 1.3-alpha |
---|
comment:2 by , 14 years ago
Cc: | added |
---|---|
Component: | Uncategorized → Database layer (models, ORM) |
comment:3 by , 14 years ago
comment:4 by , 14 years ago
Resolution: | → worksforme |
---|---|
Status: | new → closed |
Neither do I... closing it as worksforme
comment:5 by , 14 years ago
Resolution: | worksforme |
---|---|
Status: | closed → 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 by , 14 years ago
Resolution: | → worksforme |
---|---|
Status: | reopened → 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.
I can't reproduce this in 1.2 or trunk.