问题描述
我正在创建一个跟踪关于数据库中其他所谓 UUIDSyncable
对象的创建,更新和删除的更改(更新)的对象。
I am creating an object which tracks changes (Updates) regarding the creation, updating and deletion of other so called UUIDSyncable
objects in the database.
这涉及扩展 UUIDSyncable
类的 save()
和 delete()
方法被覆盖,从而创建一个新的更新
对象记录动作(插入,更新或者删除),并且最初是一个 models.CharField(max_length = 64)
字段,指定 UUIDSyncable
的UUID pk对象。
This involves any object which extends the UUIDSyncable
classes's save()
and delete()
methods being overriden, in such a way that it creates a new Update
object recording the action (either insert, update or delete) and originally a models.CharField(max_length=64)
field specifying the UUID pk of of the of UUIDSyncable
objects.
此时我想更新实现,以使用Django的 generic.GenericForeignKey()
字段替代我的单字字段。这将使得以更简单的方式访问更新记录的对象。
At this point I wanted to update the implementation to use Django's generic.GenericForeignKey()
field in stead of my single character field. This would enable be to access the object about which the update is being recorded in a much simpler way.
当您想删除一个对象并将其存储为一个更新
对象,因为你知道Django在 UUIDSyncable
对象被删除之前预先设置级联删除,删除所有以前的更新
记录(不需要)。
My problem arrises when you wish to delete an object and store that as an Update
object, as you know Django preforms a cascade delete on the UUIDSyncable
object being deleted, deleting all the previous Update
records (which is not wanted).
当 UUIDSyncable
对象在我的原始实现中被删除,每个更新
对象仍然保留UUID以通知外部方关于它的删除。我的第一个模拟解决方案是在删除所有更新
对象之间的对象之前设置 content_object = None
,因此阻止删除级联。
When the UUIDSyncable
object has been deleted in my original implementation, each Update
object would still retain the UUID to inform external parties about its deletion. My first solution to emulate this was to set content_object = None
just before deleting the object on all the Update
objects, thus preventing the deletion cascade.
它接收 generic.GenericForeignKey()
不能设置为允许 NULL
值。有没有办法做到这一点,或者如果没有标准的 CharField
持有UUID主键和一个sperate CharField
持有模型名称以类似的方式使用
It seams that generic.GenericForeignKey()
cannot be set to allow NULL
values. Is there a way to do this, or if not how can a standard CharField
holding a UUID primary key and a sperate CharField
holding the model name be used in a similar fashion?
推荐答案
也许您需要在底层的 content_type
和 object_id
组成通用外键的字段。
Perhaps you need to set null=True on the underlying content_type
and object_id
fields that make up the generic foreign key.
这篇关于generic.GenericForeignKey()字段可以为空吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!