#14438: Deserializer raises ValidationError if natural key is given as string
-------------------------------------+-------------------------------------
     Reporter:  zimnyx               |                    Owner:  nobody
         Type:  Bug                  |                   Status:  reopened
    Component:  Core                 |                  Version:  1.2
  (Serialization)                    |               Resolution:
     Severity:  Normal               |             Triage Stage:
     Keywords:                       |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by Dmitry Kaloshkin <demon.koshkin@...>):

 * status:  closed => reopened
 * severity:   => Normal
 * resolution:  worksforme =>
 * version:  SVN => 1.2
 * easy:   => 0
 * ui_ux:   => 0
 * type:   => Bug


Comment:

 This is my very first post to Django bug system, so in case smth is
 incorrect please do not beat a newbie and let me know what's wrong in this
 bug submission form.

 I am using Django 1.2.7.

 When loading fixtures from json file I had the same issue.

 First, I have loaded fixtures automatically by using syncdb. Got
 ValidationError.

 Second, I manually deserialized json file and got the same
 ValidationError.

 >>> f = open('/home/dima/djprojects/mysite/places/data/data_chunk.json',
 'r')
 >>> for object in serializers.deserialize('json', f):
 ...     print object
 ...
 <DeserializedObject: places.MenuCategory(pk=9)>
 <DeserializedObject: places.MenuCategory(pk=10)>
 Traceback (most recent call last):
   File "<console>", line 1, in <module>
   File "/usr/local/lib/python2.6/dist-
 packages/django/core/serializers/json.py", line 35, in Deserializer
     for obj in PythonDeserializer(simplejson.load(stream), **options):
   File "/usr/local/lib/python2.6/dist-
 packages/django/core/serializers/python.py", line 119, in Deserializer
     value =
 field.rel.to._meta.get_field(field.rel.field_name).to_python(field_value)
   File "/usr/local/lib/python2.6/dist-
 packages/django/db/models/fields/__init__.py", line 471, in to_python
     raise exceptions.ValidationError(self.error_messages['invalid'])
 ValidationError: [u'This value must be an integer.']

 I have verified that my natural_key() model method is returning a tuple,
 but this did not fix the bug.

 class MenuCategoryManager(models.Manager):
     def get_by_natural_key(self, category):
         return self.get(category=category)

 class MenuCategory(models.Model):
     objects = MenuCategoryManager()
     category = models.CharField(max_length=256)

     # definition of natural keys
     def natural_key(self):
         return (self.category,)

 Only when I added [] to the value in json file (where key was referring to
 the ForeignKey relationship) the Error resolved.

 So, I was get ValidationError with this json

         "menu_category": "Холодные закуски и салаты",

 And no ValidationError with this json

         "menu_category": [
             "Холодные закуски и салаты"
         ],

-- 
Ticket URL: <https://code.djangoproject.com/ticket/14438#comment:3>
Django <https://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 [email protected].
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