Opened 10 years ago

Closed 9 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


Command 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 Changed 10 years ago by Piotr Czachur

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

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


    colors: [red, green, blue]

will also fail with similar ValidationError.

comment:2 Changed 10 years ago by Ramiro Morales

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:
    - [add_user, auth, user]
    - [change_user, auth, user]
    - [delete_user, auth, user]
    person: [Django Reinhardt]
  pk: 1
- fields:
    - [add_user, auth, user]
    person: [Stephane Grappelli]
  pk: 2
- fields:
    permissions: []
    person: [Prince]
  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:

comment:3 Changed 9 years ago by Dmitry Kaloshkin <demon.koshkin@…>

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/", line 35, in Deserializer
    for obj in PythonDeserializer(simplejson.load(stream), **options):
  File "/usr/local/lib/python2.6/dist-packages/django/core/serializers/", line 119, in Deserializer
    value =
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/fields/", 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": [
            "Холодные закуски и салаты"
Last edited 9 years ago by Karen Tracey (previous) (diff)

comment:4 Changed 9 years ago by kenth

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