修订控制模型的好方法? - 姜戈
这是我的尝试:
示例#1
class RevisionControlledValue(models.Model):
created = models.DateTimeField(auto_now_add=True)
created_by = models.ForeignKey(User)
value = models.TextField()
class Meta:
ordering = ('-created', )
class DjangoPony(models.Model):
names = models.ManyToManyField(RevisionControlledValue, \
related_name="fromname_djangopony_set")
colors = models.ManyToManyField(RevisionControlledValue, \
related_name="fromcolor_djangopony_set")
@property
def name(self):
return self.names.latest('created')
示例#2
class RevisionControlledValue(models.Model):
created = models.DateTimeField(auto_now_add=True)
created_by = models.ForeignKey(User)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
value = models.TextField()
class Meta:
ordering = ('-created', )
class DjangoPony(models.Model):
@property
def name(self):
return self.revisioncontrolledvalue.latest('created')
两者对我来说都感觉有点不确定,尤其是 content_type ,我认为这应该是一个有利的选择。
对于 content_type ,模型中不存在不同类型的修订控制字段的知识,如果我想要不同类型的字段怎么办?
对于第一个多对多并不真正符合逻辑,因为它是一对多关系。
有什么建议或想法吗? :)
here is my attempt:
Example #1
class RevisionControlledValue(models.Model):
created = models.DateTimeField(auto_now_add=True)
created_by = models.ForeignKey(User)
value = models.TextField()
class Meta:
ordering = ('-created', )
class DjangoPony(models.Model):
names = models.ManyToManyField(RevisionControlledValue, \
related_name="fromname_djangopony_set")
colors = models.ManyToManyField(RevisionControlledValue, \
related_name="fromcolor_djangopony_set")
@property
def name(self):
return self.names.latest('created')
Example #2
class RevisionControlledValue(models.Model):
created = models.DateTimeField(auto_now_add=True)
created_by = models.ForeignKey(User)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
value = models.TextField()
class Meta:
ordering = ('-created', )
class DjangoPony(models.Model):
@property
def name(self):
return self.revisioncontrolledvalue.latest('created')
Both feel a bit iffy to me, especially the content_type one, which I would think should be favorable option.
With the content_type one, there is no knowledge within the model of the different types of revision controlled fields and what if I want different types of fields?
For the first one Many to Many does not really match the logic, as it is a One to Many relationship.
Any tips or ideas? :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有一个用于 django-reversion 的应用程序。如果您确实只想对一个字段进行还原控制,请将其分成自己的表并建立一对一的关系。
There is app for that django-reversion. If you really only want to have reversion control on one field break it off into its own table and make a 1-to-1 relationship.
我认为您忘记了标题中的“字段”一词。 =)我想我会使用一种保持原始模型完整的方法。开头为:
这假设您仅将其用于文本字段。为了使其真正美观,您可以创建一个新字段,例如
ReversionTextField
,它会在保存模型时自动创建反转对象。I think you forgot the word fields in your title. =) I guess I would use an approach that leaves the original model intact. Something starting with:
This assumes you'll only be using it for TextFields. To make it really beautiful, you could create a new field, let's say
ReversionTextField
that automatically creates the reversion object when the model is saved.@Jason
你不需要创建新的模型......从回归文档:
保存字段的子集
如果您只想将字段的子集保存到修订版中,则可以为 register 方法指定 fields 参数。
回归经过了很好的测试,低级 API 可以让你做我想做的几乎所有事情。
@Jason
You don't need to create new models... from the reversion docs:
Saving a subset of fields
If you only want a subset of fields to be saved to a revision, you can specify a fields argument to the register method.
Reversion is pretty well tested and the low level api lets you do pretty much all I've ever wanted.