Django 1.4.1 and using natural keys in deserialization

Following the Django docs on deserialization, in mine

I created a class:

class PersonManager(models.Manager):
    def get_by_natural_key(self, name):
        return self.get(name=name)

class Person(models.Model):
    objects = PersonManager()
    name = models.CharField(max_length=30, unique=True, blank=False, null=False)

    def __unicode__(self):

class Book(models.Model):
    title = models.CharField(max_length=100)
    author = models.ForeignKey(Person)

    def __unicode__(self):
            return self.title


I also created a tool for Person, person.json,

        "pk": null,
        "model": "myapp.person", 
        "fields": {
            "name": "josh",


and for the book "book.json",

        "pk": null,
        "model": "", 
        "fields": {
            "title": "my book",
            "author": ["josh"]


The luminaires are saved in the luminaires folder.

Then I execute python sql myapp

and python syncdb

and then python loaddata persondata.json

. It works. (I can see the entered data on the admin page and I get a message with a good amount of insert).

Then I do python loaddata bookdata.json

and get the following error message:

DeserializationError: [u"'[u'josh']' value must be an integer."]

How can it be? Why does Django insist that I use an integer for the primary key when I declare it PersonManager

? What did I miss?

(NB. This all works fine when I give author


directly as an integer.)


source to share

1 answer

Your manager has the wrong name. It should be get_by_natural_key

, not get_by_unique_key




All Articles