#28742 closed Bug (fixed)
get/set on a OneToOneField produces AttributeError
Reported by: | Faidon Liambotis | Owned by: | Paulo |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.11 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
On a sample empty project (mysite) with a sample empty app (myapp) with these models:
from django.db import models class Place(models.Model): name = models.CharField(max_length=50) address = models.CharField(max_length=80) class Restaurant(models.Model): place = models.OneToOneField(Place) serves_hot_dogs = models.BooleanField(default=False) serves_pizza = models.BooleanField(default=False)
Executing this, works just fine:
p = Place() p.restaurant = None
But this:
p = Place() r = getattr(p, 'restaurant', None) p.restaurant = None
…raises an AttributeError:
AttributeError Traceback (most recent call last) <ipython-input-4-c4df23702cac> in <module>() ----> 1 p.restaurant = None .../lib/python3.5/site-packages/django/db/models/fields/related_descriptors.py in __set__(self, instance, value) 435 else: 436 delattr(instance, self.cache_name) --> 437 setattr(rel_obj, self.related.field.name, None) 438 elif not isinstance(value, self.related.related_model): 439 # An object must be an instance of the related class. AttributeError: 'NoneType' object has no attribute 'place'
The above is with Python 3.5.3 and Django 1.11.6, although user "knbk" on #django said that they reproduced it in master.
Change History (8)
comment:1 by , 7 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:2 by , 7 years ago
comment:3 by , 7 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:4 by , 7 years ago
PR: https://github.com/django/django/pull/9305
I think the regression came from https://github.com/django/django/commit/384ddbec1b73a4636f234da3894fde8f8420bb63
comment:5 by , 7 years ago
Has patch: | set |
---|
The test in the PR is failing as far back as I tested (Django 1.7, note: you have to update the models according to 7fe554b2a3d72d0142e5c9e97efbd0a672c2d790 there).
Are you seeing a regression from an older version of Django?
comment:6 by , 7 years ago
I didn't test that far back.
Only traced the AttributeError to the commit above, before that I assume a ValueError was raised because setting None was just not allowed.
So is not really a regression.
Another way of reproducing this is with
The problem is that we are caching the return of
Restaurant.DoesNotExist
. Removing the caching triggers a regression test for #17439. I saw that Aymeric closed that one quite a while ago so I'd be interested on some additional thoughts.