﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
9649	[bug] invalid attribute value passed form model constructor	Ales Zoulek	nobody	"'''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
}}}
"		closed	Database layer (models, ORM)	1.0		fixed	bug model orm foreignkey	ales.zoulek@… ondrej.kohout@…	Accepted	0	0	0	0	0	0
