Django Deserializer: Problem installing fixture. Must be an integer

Viewed 222

I'm trying to load fixtures from a json file (using loaddata) - the json file was generated using django's dumpdata, but two FK fields (returned as one of their unique fields) throw up errors. Django is looking for PKs, but the JSON has strings and hence the error. Here's my code:

Model1 - AreaProductStock and AreaProductStockManager

class AreaProductStockManager(models.Manager):
    def get_by_natural_key(self, code, inv_code):
        self.get(area__code=code, product__inv_code=inv_code)

class AreaProductStock(models.Model):
    objects = AreaProductStockManager()
    area = models.ForeignKey('Area', null=True, blank=True)
    product = models.ForeignKey('Product', related_name='area_product_stocks', null=True, blank=True)
​
    def natural_key(self):
        return (self.area.code, self.product.inv_code,)    # Returns a tuple as expected

Model2: Area and AreaManager

class AreaManager(models.Manager):
    def get_by_natural_key(self, code):
        self.get(code=code)

class Area(models.Model):
    CA = 'CA'
    MA = 'MA'
​
    AREA_CHOICES = [(CA, 'CA'), (MA, 'MA')]
    code = models.CharField(max_length=2, choices=AREA_CHOICES, db_index=True, unique=True)

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

Model3 - Product and ProductManager

class ProductManager(models.Manager):
    def get_by_natural_key(self, inv_code):
        self.get(inv_code=inv_code)

class Product(models.Model):
    inv_code = models.CharField(max_length=99, blank=True, null=True, db_index=True, unique=True)

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

Generated JSON:

    [
  {
    "fields": {
      "product": [
        "SOME-INV-CODE"
      ],
      "q_on_so": 0,
      "area": [
        "CA"
      ],
      "current_stock": 10,
      "available_stock": 10,
      "q_on_po": 0
    },
    "model": "my_app.areaproductstock"
  }
]

Here's the error I'm getting:

django.core.serializers.base.DeserializationError: Problem installing fixture '/project_path/project/product.json': [u"'[u'SOME-INV-CODE']' value must be an integer."]
0 Answers
Related