Can I make Django ignore duplicate devices at unique constraints other than PK?

I have some basic source data that I want to include in a Django project. It is perfect as a tool, except that I can reject duplicates. This would mean that I could reload the data into an existing installation, and this would add only those objects that did not exist.

I used natural keys and foreign keys in one field, so there are no questions about collisions with identifiers, etc.

Possible? I do not want to write a JSON importer when it already exists, and my data is already in the armature.

+4
source share
1 answer

Typically, an instrument contains all of the model information, which includes a primary key. Below is an example of a device from the Django website:

[ { "model": "myapp.person", "pk": 1, "fields": { "first_name": "John", "last_name": "Lennon" } }, { "model": "myapp.person", "pk": 2, "fields": { "first_name": "Paul", "last_name": "McCartney" } } ] 

As you can see, he has a primary key. This way, even if you have duplicated fixtures, if the primary keys are the same, no matter what is imported last, it will overwrite records in db, so you should not have duplicate data in db.

+4
source

Source: https://habr.com/ru/post/1442837/


All Articles