Django 1.4.1 and using natural keys in deserialization

Following the Django docs on deserialization, in mine models.py

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):
        return self.name

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": "myapp.book", 
        "fields": {
            "title": "my book",
            "author": ["josh"]
        }
     }
]

      

The luminaires are saved in the luminaires folder.

Then I execute python manage.py sql myapp

and python manage.py syncdb

and then python manage.py 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 manage.py 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

pk

directly as an integer.)

+3


source to share


1 answer


Your manager has the wrong name. It should be get_by_natural_key

, not get_by_unique_key

.



+1


source







All Articles