django 模型中依赖字段的最佳实践
出于数据库索引的目的,模型上的电子邮件字段需要反向存储。我最终需要访问电子邮件字段的反向和转发版本。我很好奇以下哪种方法被认为是最佳实践。
方法A 重写模型上的 save
方法。此方法对数据库进行了一些非规范化,并且不适用于查询集上的 update
方法。有些需要覆盖为模型生成的表单。
class Foo(models.Model):
email_forward = models.CharField(max_length = 320)
email_reversed = models.CharField(max_length = 320)
def save(self, *args, **kwargs):
self.email_reversed = reversed(self.email_forward)
super(Foo, self).save(*args, **kwargs)
方法B 这种方式有更好的数据库规范化。仍然允许您在查询集上使用 update
方法。搞乱表单,最终导致您必须覆盖为模型生成的所有默认表单。
class Foo(models.Model):
_email = models.CharField(max_length = 320)
@property
def email_forward(self):
if not hasattr(self, 'email_f'):
self.email_f = reversed(self._email)
return self.email_f
@email.setter
def email_forward(self, value):
self.email_f = value
self._email = reversed(value)
@propery
def email_reversed(self):
return self._email
说明
任何替代答案都需要满足将反向电子邮件存储在数据库中的最低要求。然而,这个问题并不是要找到这个特定问题的答案,而是要获取有关这种场景的最佳实践的反馈,在这种场景中,您有两个可以相互计算的字段,但在前端上下文中需要一个字段,另一个在后端上下文中
For database indexing purposes, the email field on a model needs to be stored in reverse. I end up needing access to both the reversed and forward version of the email field. I'm curious as to which of the following methods would be considered best practice.
Method A
Override the save
method on the model. This method denormalizes the database some and doesn't work with the update
method on a queryset. Some need to override forms generated for the model.
class Foo(models.Model):
email_forward = models.CharField(max_length = 320)
email_reversed = models.CharField(max_length = 320)
def save(self, *args, **kwargs):
self.email_reversed = reversed(self.email_forward)
super(Foo, self).save(*args, **kwargs)
Method B
This way has better database normalization. Still allows you to use the update
method on querysets. Screws up forms so that you end up having to override all of the default forms generated for the model.
class Foo(models.Model):
_email = models.CharField(max_length = 320)
@property
def email_forward(self):
if not hasattr(self, 'email_f'):
self.email_f = reversed(self._email)
return self.email_f
@email.setter
def email_forward(self, value):
self.email_f = value
self._email = reversed(value)
@propery
def email_reversed(self):
return self._email
Clarification
Any alternative answers need to meet the minimum requirement of having the reversed email stored in the database. This question is however, not so much about finding an answer to this specific problem, but getting feedback on best practices for this sort of scenario where you have two fields which can be computed from one another, but one is required in a frontend context, and the other in a backend context
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
模型:
和表单:
不确定您所说的“搞砸表单”是什么意思,但是这个模型表单只有一个字段 -
电子邮件
。我还添加了如何使用模型的示例:The Model:
And the Form:
Not sure what you meant by "Screws up the form", but this model form will only have one field - the
email
. I have also added an example of how the models are used: