Code

Opened 4 years ago

Closed 3 years ago

#14438 closed Bug (duplicate)

Deserializer raises ValidationError if natural key is given as string

Reported by: zimnyx 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]

Attachments (0)

Change History (4)

comment:1 Changed 4 years ago by zimnyx

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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 Changed 4 years ago by ramiro

  • Resolution set to worksforme
  • Status changed from new to closed

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 Changed 3 years ago by Dmitry Kaloshkin <demon.koshkin@…>

  • Easy pickings unset
  • Resolution worksforme deleted
  • Severity set to Normal
  • Status changed from closed to reopened
  • Type set to Bug
  • UI/UX unset
  • Version changed from SVN to 1.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_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 3 years ago by kmtracey (previous) (diff)

comment:4 Changed 3 years ago by kenth

  • Resolution set to duplicate
  • Status changed from reopened to closed

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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.