Django - 具有伴随存储字段的自定义虚拟模型字段 [英] Django - Custom virtual model field with companion stored field
问题描述
我想要一个 ConfirmationField
字段类型。我希望这个字段像布尔字段一样工作。我不需要将这些信息存储在数据库中,而是我想在单独的字段中存储确认日期。
I would like to have a ConfirmationField
field type. I want this field to work like a boolean field. I don't need to store this information on database instead I would like to store the date of confirmation on a seperate field.
class MyModel(models.Model):
confirmation = ConfirmationField()
m = MyModel()
m.confirmation # False
m.confirmation_timestamp # None
m.confirmation = True
m.save()
m.confirmation_timestamp # datetime object
m.confirmation = False
m.save()
m.confirmation_timestamp # None again
我需要将这两个字段作为 Field
实例,我需要能够在Admin中使用它们。
I need to have both fields as Field
instances, I need to be able to use them in Admin for example.
我已阅读文档并检查代码, ForeignKey
使用以下代码更改其数据库列名:
I have read the docs and checked the code, ForeignKey
changes its database column name with the following code:
def get_attname(self):
return '%s_id' % self.name
但是,我无法弄清楚我如何使用其余的的代码为例。似乎我的目标不需要太多的复杂性。
But I couldn't figure out exacly how I can use the rest of the code as an example. It seems my goal doesn't require tto much complexity.
我还发现 ImageField
保存额外的信息,当模型被保存:
I have also found ImageField
saved extra information when the model is saved:
if self.field.width_field:
setattr(self.instance, self.field.width_field, self.width)
if self.field.height_field:
setattr(self.instance, self.field.height_field, self.height)
但当然,我不希望将确认
本身存储在数据库中。因为( confirmation_timestamp
- > 确认
)无
== False
和< datetime instance>
== True $ c $
But of course I don't want confirmation
itself to be stored in the database. I would cause redundancy since (confirmation_timestamp
-> confirmation
) None
==False
and <datetime instance>
==True
.
任何指针/想法都不胜感激。
Any pointers/ideas are appreciated.
谢谢。
编辑:目前看来,这是不可能的。我最终改变了我的API这是一个snipplet,给出一个想法,我是如何做到的:
edit: It appears this is not currently possible. I have ended up changing my API. Here is a snipplet to give an idea how I did it:
value = {'BooleanField': True,
'DateTimeField': datetime.datetime.now(),
'DateField': datetime.date.today(),
'IntegerField': 1,
}[instance._meta.get_field(field_name).get_internal_type()]
setattr(instance, field_name, value)
具有上述任何类型的确认字段,并设置正确的值。并且可以使用 bool(getattr(instance,field_name)
。
You can have a confirmation field any type mentioned above and it sets the correct value. And confirmation checking can be done with bool(getattr(instance, field_name)
.
推荐答案
如果我有这个权利,请考虑:
if I got this right, consider:
class Modelname(models.Model):
"""(Modelname description)"""
confirmation_timestamp = models.DateTimeField(blank=True)
def _set_confirmation(self, value):
if not value:
self.confirmation_timestamp = None
else:
self.confirmation_timestamp = datetime.datetime.now()
def _get_confirmation(self):
return bool(self.confirmation_timestamp)
confirmation = property(_get_confirmation, _set_confirmation)
confirmation.short_description = "Confirmation"
confirmation.boolean = True # have it nicely displayed in admin
这篇关于Django - 具有伴随存储字段的自定义虚拟模型字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!