Opened 14 years ago

Closed 13 years ago

#14438 closed Bug (duplicate)

Deserializer raises ValidationError if natural key is given as string

Reported by: Piotr Czachur Owned by: nobody
Component: Core (Serialization) Version: 1.2
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Command manage.py dumpdata --natural --format=yaml generates FK as following:

    fk_name: natural_key_value

for example

    color: red

Such fixture gives ValidationError when trying to load:

    ValidationError: [u'This value must be an integer.']

The workaround for this problem is put natural key in []:

    fk_name: [natural_key_value]

Change History (4)

comment:1 by Piotr Czachur, 14 years ago

and in case of m2m relation, in yaml you have to use:

    colors: [[red], [green], [blue]]

because:

    colors: [red, green, blue]

will also fail with similar ValidationError.

comment:2 by Ramiro Morales, 14 years ago

Resolution: worksforme
Status: newclosed

I've tested this locally and the YAML generated is correct (see fixtures6 and fixures8 in Django's tests/modeltests/fixtures test case, Visa has a FK to Person that has a one-field natural key).

- fields:
    permissions:
    - [add_user, auth, user]
    - [change_user, auth, user]
    - [delete_user, auth, user]
    person: [Django Reinhardt]
  model: fixtures.visa
  pk: 1
- fields:
    permissions:
    - [add_user, auth, user]
    person: [Stephane Grappelli]
  model: fixtures.visa
  pk: 2
- fields:
    permissions: []
    person: [Prince]
  model: fixtures.visa
  pk: 3

I suspect the problem is the natural_key method of the model your FK points to isn't returning a tuple. This is documented: http://docs.djangoproject.com/en/1.2/topics/serialization/#serialization-of-natural-keys

comment:3 by Dmitry Kaloshkin <demon.koshkin@…>, 13 years ago

Easy pickings: unset
Resolution: worksforme
Severity: Normal
Status: closedreopened
Type: Bug
UI/UX: unset
Version: SVN1.2

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_messagesinvalid)

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": [

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

],

Version 0, edited 13 years ago by Dmitry Kaloshkin <demon.koshkin@…> (next)

comment:4 by kenth, 13 years ago

Resolution: duplicate
Status: reopenedclosed

The problem you're running into is that your natural key is not in a "tuple". This is the same root cause of the problem in #16208. I'm closing this one duplicate & posting fix there.

Note: See TracTickets for help on using tickets.
Back to Top