简体   繁体   English

Django:loaddata 更新数据

[英]Django : loaddata to update data

I have a fixture seed_data.json on which I have my initial data.我有一个固定装置seed_data.json ,上面有我的初始数据。

Sometimes I add new data into this fixtures and reload it, which updates my data correctly.有时我将新数据添加到这个装置中并重新加载它,这会正确更新我的数据。

However, I now want to remove some data from it.但是,我现在想从中删除一些数据。 So I modified my seed_data.json , for instance, I had something like that :所以我修改了我的seed_data.json ,例如,我有这样的东西:

{"fields": {"name": "Field 0"},"model": "catalog.product","pk": 1},
{"fields": {"name": "Field 1"},"model": "catalog.product","pk": 2},
{"fields": {"name": "Field 2"},"model": "catalog.product","pk": 3},
# ...

That became :那变成了:

{"fields": {"name": "Field 1"},"model": "catalog.product","pk": 1},
{"fields": {"name": "Field 2"},"model": "catalog.product","pk": 2},
# ...

But I'm getting :但我得到:

django.db.utils.IntegrityError: Problem installing fixture .....\\seed_data.json

Could not load catalog.Product(pk=2): column name is not unique

So there's no problem when adding some data, but when trying to remove some, there are conflicts with the primary keys.所以添加一些数据时没有问题,但是尝试删除一些时,会与主键发生冲突。

How can I achieve what I'm trying to do ?我怎样才能实现我想要做的事情?

Fixtures are only for initial data for completely new database instances, eg when running tests.夹具仅用于全新数据库实例的初始数据,例如在运行测试时。 To modify existing data use migrations.要修改现有数据,请使用迁移。

Getting there after all those years and with an unresolved question makes me want to add something because I did struggle with fixtures too.在这么多年之后到达那里并且有一个未解决的问题让我想添加一些东西,因为我也确实在固定装置上挣扎。

For modifying existing data, as @Thomas said the best it to use Django data migrations .对于修改现有数据,正如@Thomas 所说,最好使用Django 数据迁移

When new to Django though, you might be tempted to use fixtures for updating some basic data.但是,当您刚接触 Django 时,您可能会尝试使用设备来更新一些基本数据。 Again, this is not recommended but it doesn't hurt to explain what can be done.同样,这不是推荐的,但解释可以做什么并没有什么坏处。 So, using the example:因此,使用示例:

{"fields": {"name": "Field 0"},"model": "catalog.product","pk": 1},
{"fields": {"name": "Field 1"},"model": "catalog.product","pk": 2},
{"fields": {"name": "Field 2"},"model": "catalog.product","pk": 3},
# ...

That became :那变成了:

{"fields": {"name": "Field 1"},"model": "catalog.product","pk": 1},
{"fields": {"name": "Field 2"},"model": "catalog.product","pk": 2},
# ...

Here, the issue is indeed to try to update an object with a primary key that already exists on another object (object with pk = 3 ).在这里,问题确实是尝试使用已存在于另一个对象( pk = 3的对象)上的主键更新对象。 You cannot delete the object with pk = 3 just with the fixture.您不能仅使用夹具删除pk = 3的对象。 If you do it though, then the updated fixture above would have worked.如果你这样做了,那么上面更新的装置就会起作用。

Here are some rules that can help.以下是一些可以提供帮助的规则。

What you can do when modifying a fixture and calling loaddata on it:修改夹具并在其上调用loaddata时可以执行的操作:

  • Update data (as long as it respects unicity constraints).更新数据(只要它尊重唯一性约束)。 Django will use pk to match object and will replace the previous object without calling any pre_save functions (so that's another reason why it's not recommended to use it) Django 会使用pk来匹配对象,并且会在不调用任何pre_save函数的情况下替换之前的对象(所以这是不建议使用它的另一个原因)
  • Create data (with the same constraints as before)创建数据(具有与以前相同的约束)

What you cannot do when modifying a fixture and calling loaddata on it:修改夹具并在其上调用loaddata时不能执行的操作:

  • You cannot explicitly delete an object with a fixture.您不能显式删除带有夹具的对象。 As the pk of the object you want to delete won't be in the file, it will stay as is in the database.由于您要删除的对象的pk不在文件中,因此它将保持在数据库中。 So you have to delete it manually.所以你必须手动删除它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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