djangodjango-modelsprimary-keynatural-key

Django fixtures primary key error, need natural keys solution


So I have a Film model that holds a list of Actors model in a many to many field:

class Person(models.Model):
    full = models.TextField()
    short = models.TextField()
    num = models.CharField(max_length=5)

class Film(models.Model):
    name = models.TextField()
    year = models.SmallIntegerField(blank=True)
    actors = models.ManyToManyField('Person')

I'm trying to load some initial data from json fixtures, however the problem I have is loading the many to many actors field. For example I get the error:

DeserializationError: [u"'Anna-Varney' value must be an integer."]

with these fixtures:

  {
    "pk": 1,
    "model": "data.Film",
    "fields": {
      "actors": [
        "Anna-Varney"
      ],
      "name": "Like a Corpse Standing in Desperation (2005) (V)",
      "year": "2005"
    }

while my actors fixture looks like this:

 {
    "pk": 1,
    "model": "data.Person",
    "fields": {
      "full": "Anna-Varney",
      "num": "I",
      "short": "Anna-Varney"
    }
  }

So the many to many fields must use the pk integer, but the problem is that the data isn't sorted and for a long list of actors I don't think its practical to manually look up the pk of each one. I've been looking for solutions and it seems I have to use natural keys, but I'm not exactly sure how to apply those for my models.

EDIT: I've changed my models to be:

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

class Person(models.Model):
    objects = PersonManager()
    full = models.TextField()
    short = models.TextField()
    num = models.CharField(max_length=5)
    def natural_key(self):
        return self.full

But I'm still getting the same error


Solution

  • There's a problem with both the input and the natural_key method.

    Documentation: Serializing Django objects - natural keys states:

    A natural key is a tuple of values that can be used to uniquely identify an object instance without using the primary key value.

    The Person natural_key method should return a tuple

    def natural_key(self):
        return (self.full,)
    

    The serialised input should also contain tuples/lists for the natural keys.

    {
        "pk": 1, 
        "model": "data.film", 
        "fields": {
            "actors": [
                [
                    "Matt Damon"
                ], 
                [
                    "Jodie Foster"
                ]
            ], 
            "name": "Elysium", 
            "year": 2013
        }
    }