#9649 closed (fixed)
[bug] invalid attribute value passed form model constructor
Reported by: | Ales Zoulek | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.0 |
Severity: | Keywords: | bug model orm foreignkey | |
Cc: | ales.zoulek@…, ondrej.kohout@… | Triage Stage: | Accepted |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
How to simulate
models.py
from django.db import models class CModel(models.Model): x = models.CharField(max_length=20) def __unicode__(self): return self.x class AModel(models.Model): a = models.FloatField() b = models.FloatField() c = models.ForeignKey(CModel) def __unicode__(self): return u"%s" % self.a
shell:
In [1]: from t.a import models In [2]: models.AModel(a=23.0, c=None).c_id In [3]: models.AModel(b=23.0, c=None).c_id Out[3]: 23.0
Reason
Invalid if clause in django/db/models/base.py:242
When rel_obj is set to None, but field.null is set to False, the 'val' variable is NOT set in the whole FOR-cycle and thus the previous value gets used.
Solution
insead of:
242 if rel_obj is None and field.null: 243 val = None
there should be:
242 if rel_obj is None: 243 val = None 244 if not field.null: 245 raise TypeError, "Attribute %s could not be null" % field.name
Change History (5)
comment:1 by , 16 years ago
Cc: | added |
---|
comment:2 by , 16 years ago
milestone: | → 1.1 |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:3 by , 16 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
(In [9983]) Fixed #9649 -- Better error handling in model creation.
Previously, you could explicitly assign None to a non-null ForeignKey
(or other) field when creating the model (Child(parent=None), etc). We
now throw an exception when you do that, which matches the behaviour
when you assign None to the attribute after creation.
Thanks to ales.zoulek@… and ondrej.kohout@… for some
analysis of this problem.
comment:4 by , 16 years ago
(In [9984]) [1.0.X] Fixed #9649 -- Better error handling in model creation.
Previously, you could explicitly assign None to a non-null ForeignKey
(or other) field when creating the model (Child(parent=None), etc). We
now throw an exception when you do that, which matches the behaviour
when you assign None to the attribute after creation.
Thanks to ales.zoulek@… and ondrej.kohout@… for some
analysis of this problem.
Backport of r9983 from trunk.
Validating of assigned values should be done by Filed, not by a Model
Alternative solution:
Then: