#9649: [bug] invalid attribute value passed form model constructor
---------------------------------------------------+------------------------
          Reporter:  ales_zoulek                   |         Owner:  nobody     
             
            Status:  new                           |     Milestone:             
             
         Component:  Database layer (models, ORM)  |       Version:  1.0        
             
        Resolution:                                |      Keywords:  bug model 
orm foreignkey
             Stage:  Unreviewed                    |     Has_patch:  0          
             
        Needs_docs:  0                             |   Needs_tests:  0          
             
Needs_better_patch:  0                             |  
---------------------------------------------------+------------------------
Changes (by iky <[EMAIL PROTECTED]>):

 * cc: [EMAIL PROTECTED] (added)
  * needs_better_patch:  => 0
  * needs_tests:  => 0
  * needs_docs:  => 0

Comment:

 Validating of assigned values should be done by Filed, not by a Model

 Alternative solution:
 {{{
 +++ django/db/models/base.py    (working copy)
 @@ -224,7 +224,7 @@
          # keywords, or default.

          for field in fields_iter:
 -            rel_obj = None
 +            rel_obj = False
              if kwargs:
                  if isinstance(field.rel, ManyToOneRel):
                      try:
 @@ -249,7 +249,7 @@
              # instead of field.attname (e.g. "user" instead of "user_id")
 so
              # that the object gets properly cached (and type checked) by
 the
              # RelatedObjectDescriptor.
 -            if rel_obj:
 +            if rel_obj is not False:
                  setattr(self, field.name, rel_obj)
              else:
                  setattr(self, field.attname, val)
 }}}

 Then:

 {{{
 In [2]: models.AModel(b=23.0, c=None).c_id
 ---------------------------------------------------------------------------
 ValueError                                Traceback (most recent call
 last)

 ...<ipython console> in <module>()

 ...django/django/db/models/base.pyc in __init__(self, *args, **kwargs)
     251             # RelatedObjectDescriptor.
     252             if rel_obj is not False:
 --> 253                 setattr(self, field.name, rel_obj)
     254             else:
     255                 setattr(self, field.attname, val)

 ...django/django/db/models/fields/related.pyc in __set__(self, instance,
 value)
     259         if value is None and self.field.null == False:
     260             raise ValueError('Cannot assign None: "%s.%s" does not
 allow null values.' %
 --> 261                                 (instance._meta.object_name,
 self.field.name))
     262         elif value is not None and not isinstance(value,
 self.field.rel.to):
     263             raise ValueError('Cannot assign "%r": "%s.%s" must be
 a "%s" instance.' %

 ValueError: Cannot assign None: "AModel.c" does not allow null values.
 }}}

-- 
Ticket URL: <http://code.djangoproject.com/ticket/9649#comment:1>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-updates@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to