简体   繁体   中英

Error when importing django fixture with natural key

In context of a django project, I want to create some sample data in forme of a fixture. I have export my data with natural key to avoid any data overnight at the import but I'm not able to import data at all.

The fixture has been created with the following command :

python manage.py dumpdata --format yaml --natural-primary --natural-foreign conformity.policy conformity.measure  > conformity/fixture/NIST.yaml

I have tried to import the fixture with the following command :

python manage.py loaddata conformity/fixture/NIST.yaml

but I have the following error

django.core.serializers.base.DeserializationError: Problem installing fixture 'conformity/fixture/NIST.yaml': ['“NIST Cybersecurity Framwork” value must be an integer.']: (conformity.measure:pk=None) field_value was 'NIST Cybersecurity Framwork'

It seems to me that django don't understand data correctly and try to use natural key directly as a primary key. I haven't found any option to change this behavior.

Of course YAML file don't have any pk for the object to be imported (du to natural key), I was expected Django to automatically generate them.

edit:

I have add the Manager class and the get_by_natural_key method recomanded by Abdul Aziz Barkat comment.

class MeasureManager(models.Manager):
    def get_by_natural_key(self, name):
        return self.get(name=name)


class Measure(models.Model):
[...]
    objects = MeasureManager()

    def natural_key(self):
        return (self.name,)
    natural_key.dependencies = ['conformity.policy']

but I still have the same issue. Do you have any clue ?

Two problem in one here, first, as pointed by Abdul Aziz Barkat , it keeded to implement the Manager class and the get_by_natural_key methode for my use case. I havent hunderstand correctly the documentation on this point.

second, the data exported by the dumpdata commande is not exactly the same, so I had to reimport an old version (with pk) re exported (without pk) and it work perfectly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM