I have a Recipe
model and Label
model referring to the former with a OneToOneField
. I put managers and natural_key
methods to export both models with JSON encoding.
class RecipeManager(models.Manager):
def get_by_natural_key(self, name):
return self.get(name=name)
class Recipe(models.Model):
objects = RecipeManager()
name = models.CharField(max_length=255)
def natural_key(self):
return (self.name)
class LabelManager(models.Manager):
def get_by_natural_key(self, recipe):
return self.get(recipe=recipe)
class Label(models.Model):
objects = LabelManager()
recipe = models.OneToOneField(Recipe, primary_key=True)
name = models.CharField(max_length=255)
def natural_key(self):
return self.recipe.natural_key()
natural_key.dependencies = ['labels.recipe']
I export the Label
queryset using natural keys:
with open(l_filename, 'w') as l_file:
serialize('json',
Label.objects.all(),
indent=2,
use_natural_foreign_keys=True,
use_natural_primary_keys=True,
stream=l_file)
Everything works fine but the serialized JSON objects have no field to Recipe
model they should refer to.
[{"fields": {"name": null},"model": "labels.label"}]
Django docs, as of 1.7, doesn't give any hint specific to one-to-one relations with natural keys. Any advise?