django 模型中依赖字段的最佳实践

发布于 2024-12-05 22:50:13 字数 1176 浏览 0 评论 0原文

出于数据库索引的目的,模型上的电子邮件字段需要反向存储。我最终需要访问电子邮件字段的反向和转发版本。我很好奇以下哪种方法被认为是最佳实践。

方法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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧城烟雨 2024-12-12 22:50:13

模型:

class Foo(models.Model):
    email = models.CharField(max_length=320)

    def _get_email_reversed(self):
        return self.email[::-1]

    def _set_email_reversed(self, value):
        self.email = value[::-1]

    email_reversed=property(_get_email_reversed, _set_email_reversed)

和表单:

class FooForm(forms.ModelForm):    
    class Meta:
        model = Foo

不确定您所说的“搞砸表单”是什么意思,但是这个模型表单只有一个字段 - 电子邮件。我还添加了如何使用模型的示例:

def test_foo(self):
    foo = Foo()
    foo.email = 'moc.elpmaxe@liametset'
    self.assertEquals(foo.email_reversed, '[email protected]')

def test_set_foo_reversed(self):
    foo = Foo()
    foo.email_reversed = '[email protected]'
    self.assertEquals(foo.email, 'moc.elpmaxe@liametset') 

The Model:

class Foo(models.Model):
    email = models.CharField(max_length=320)

    def _get_email_reversed(self):
        return self.email[::-1]

    def _set_email_reversed(self, value):
        self.email = value[::-1]

    email_reversed=property(_get_email_reversed, _set_email_reversed)

And the Form:

class FooForm(forms.ModelForm):    
    class Meta:
        model = Foo

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:

def test_foo(self):
    foo = Foo()
    foo.email = 'moc.elpmaxe@liametset'
    self.assertEquals(foo.email_reversed, '[email protected]')

def test_set_foo_reversed(self):
    foo = Foo()
    foo.email_reversed = '[email protected]'
    self.assertEquals(foo.email, 'moc.elpmaxe@liametset') 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文