1

I'm having trouble with fixtures/natural keys in Django. I think I've addressed most of the usual problems, like those specified in this answer.

This might all be a red herring, as get_by_natural_key works just fine in the shell:

>>> artifact = Artifact.objects.get_by_natural_key('PP_0772')
>>> artifact
<Artifact: PP_0772: Test Artifact>

What am I doing wrong with the Fixtures?

Django 1.6

models.py

... 

class ArtifactManager(models.Manager):
    def get_by_natural_key(self, code_number_):
        return self.get(code_number=code_number_)

class Artifact(models.Model):
    def __unicode__(self):
        return self.code_number + ": " + self.title
    def natural_key(self):
        return (self.code_number,)
    objects = ArtifactManager()

    title = models.CharField(max_length = 100, default = 'Untitled')
    code_number = models.CharField('Code Number',
        max_length = 10, unique = True)

class Picture(models.Model):
    def __unicode__(self):
        return self.artifact.code_number + ": " + self.title
    def get_file_name(self):
        return basename(self.image.name)
    def upload_path(instance, image_name):
        output = settings.MEDIA_ROOT + '/uploads/' + image_name
        return output

    image = models.ImageField(upload_to = upload_path, max_length=100,
        blank=False, null=False)
    artifact = models.ForeignKey('Artifact', blank=False, null=False)
    title = models.CharField(max_length = 100, default = 'Primary',
        blank=True, null=True)

... 

My fixture: pictures.yaml

- pk: 1
  model: collection.picture
  fields: 
    image: /Users/me/images/image.jpg
    artifact: PP_0772
    title: A great image
- pk: 2
  model: collection.picture
  fields: 
    image: /Users/me/images/another_image.jpg
    artifact: PP_0775
    title: A terrific image

...

Error

So, when I run ./manage.py loaddata pictures, I get:

DeserializationError: Problem installing fixture 'pictures.yaml':
[u"'PP_0772' value must be an integer."]
4

1 回答 1

1

It turns out the problem was with my fixtures. Natural keys, I guess, need to be in a list form; when it says u"'PP_0772' value must be an integer.", it's a bit of a misnomer. Maybe that refers to the list position.

So, it should look like this:

- pk: 1
  model: collection.picture
  fields: 
    image: /Users/me/images/image.jpg
    artifact:
      - PP_0772
    title: A great image
- pk: 2
  model: collection.picture
  fields: 
    image: /Users/me/images/another_image.jpg
    artifact:
      - PP_0775
    title: A terrific image
...

Hope this helps somebody in similar straits.

于 2014-07-10T20:24:02.723 回答