(Django) 将字符串保存到 EmailField 会引发“以 10 为基数的 int() 的文字无效”

发布于 2024-09-09 04:48:55 字数 3009 浏览 9 评论 0原文

在我的用户数据编辑表单中,我有一个布尔字段,用于在新闻通讯中添加/删除电子邮件。我为此表单编写了一个自定义保存方法:

def save(self, *args, **kwargs):      
    mail = None
    if self.cleaned_data['inf_newsletter']:
        mail = NewsletterEmails(self.instance.user.email)
        mail.save() 
    else:
        try:
            mail = NewsletterEmails.objects.get(email=self.instance.user.email)
        except NewsletterEmails.DoesNotExist:
            pass
        if mail:
            mail.delete()

问题是,当我添加新的 NewsletterEmail 对象时,我将从用户电子邮件中读取的字符串值发送到电子邮件字段中,这会引发错误(或者至少我这么认为):

Traceback:
File "/home/myapp/site-packages/django/core/handlers/base.py" in get_response
  92.                 response = callback(request, *callback_args, **callback_kwargs)
File "/home/myapp/site-packages/django/contrib/auth/decorators.py" in __call__
  78.             return self.view_func(request, *args, **kwargs)
File "/home/myapp/accounts/views.py" in profile_edit
  95.             form.save()
File "/home/myapp/accounts/forms.py" in save
  58.             mail.save() 
File "/home/myapp/site-packages/django/db/models/base.py" in save
  410.         self.save_base(force_insert=force_insert, force_update=force_update)
File "/home/myapp/site-packages/django/db/models/base.py" in save_base
  470.                         manager.filter(pk=pk_val).extra(select={'a': 1}).values('a').order_by())):
File "/home/myapp/site-packages/django/db/models/manager.py" in filter
  129.         return self.get_query_set().filter(*args, **kwargs)
File "/home/myapp/site-packages/django/db/models/query.py" in filter
  498.         return self._filter_or_exclude(False, *args, **kwargs)
File "/home/myapp/site-packages/django/db/models/query.py" in _filter_or_exclude
  516.             clone.query.add_q(Q(*args, **kwargs))
File "/home/myapp/site-packages/django/db/models/sql/query.py" in add_q
  1675.                             can_reuse=used_aliases)
File "/home/myapp/site-packages/django/db/models/sql/query.py" in add_filter
  1614.                 connector)
File "/home/myapp/site-packages/django/db/models/sql/where.py" in add
  56.                 obj, params = obj.process(lookup_type, value)
File "/home/myapp/site-packages/django/db/models/sql/where.py" in process
  269.                 params = self.field.get_db_prep_lookup(lookup_type, value)
File "/home/myapp/site-packages/django/db/models/fields/__init__.py" in get_db_prep_lookup
  210.             return [self.get_db_prep_value(value)]
File "/home/myapp/site-packages/django/db/models/fields/__init__.py" in get_db_prep_value
  361.         return int(value)

Exception Type: ValueError at /user/profile/
Exception Value: invalid literal for int() with base 10: '[email protected]'

我的 NewsletterEmail 模型:

class NewsletterEmails(models.Model):
    email = models.EmailField(_(u"Email"),)

如何解决这个问题?

In my user's data edit form I have a boolean field, for adding/removing email from newsletter. I've written a custom save method for this form :

def save(self, *args, **kwargs):      
    mail = None
    if self.cleaned_data['inf_newsletter']:
        mail = NewsletterEmails(self.instance.user.email)
        mail.save() 
    else:
        try:
            mail = NewsletterEmails.objects.get(email=self.instance.user.email)
        except NewsletterEmails.DoesNotExist:
            pass
        if mail:
            mail.delete()

Problem is,that when I'm adding new NewsletterEmail object, I'm sending string value read from user's email to be saved in email field, which raises error (or at least I think so) :

Traceback:
File "/home/myapp/site-packages/django/core/handlers/base.py" in get_response
  92.                 response = callback(request, *callback_args, **callback_kwargs)
File "/home/myapp/site-packages/django/contrib/auth/decorators.py" in __call__
  78.             return self.view_func(request, *args, **kwargs)
File "/home/myapp/accounts/views.py" in profile_edit
  95.             form.save()
File "/home/myapp/accounts/forms.py" in save
  58.             mail.save() 
File "/home/myapp/site-packages/django/db/models/base.py" in save
  410.         self.save_base(force_insert=force_insert, force_update=force_update)
File "/home/myapp/site-packages/django/db/models/base.py" in save_base
  470.                         manager.filter(pk=pk_val).extra(select={'a': 1}).values('a').order_by())):
File "/home/myapp/site-packages/django/db/models/manager.py" in filter
  129.         return self.get_query_set().filter(*args, **kwargs)
File "/home/myapp/site-packages/django/db/models/query.py" in filter
  498.         return self._filter_or_exclude(False, *args, **kwargs)
File "/home/myapp/site-packages/django/db/models/query.py" in _filter_or_exclude
  516.             clone.query.add_q(Q(*args, **kwargs))
File "/home/myapp/site-packages/django/db/models/sql/query.py" in add_q
  1675.                             can_reuse=used_aliases)
File "/home/myapp/site-packages/django/db/models/sql/query.py" in add_filter
  1614.                 connector)
File "/home/myapp/site-packages/django/db/models/sql/where.py" in add
  56.                 obj, params = obj.process(lookup_type, value)
File "/home/myapp/site-packages/django/db/models/sql/where.py" in process
  269.                 params = self.field.get_db_prep_lookup(lookup_type, value)
File "/home/myapp/site-packages/django/db/models/fields/__init__.py" in get_db_prep_lookup
  210.             return [self.get_db_prep_value(value)]
File "/home/myapp/site-packages/django/db/models/fields/__init__.py" in get_db_prep_value
  361.         return int(value)

Exception Type: ValueError at /user/profile/
Exception Value: invalid literal for int() with base 10: '[email protected]'

My NewsletterEmail model :

class NewsletterEmails(models.Model):
    email = models.EmailField(_(u"Email"),)

How to get around this problem ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

绿光 2024-09-16 04:48:55
mail = NewsletterEmails(self.instance.user.email)

您将输入的电子邮件地址分配给隐式创建的主键字段,而不是电子邮件字段。

使用:

mail = NewsletterEmails(email=self.instance.user.email)
mail = NewsletterEmails(self.instance.user.email)

You are assigning the inputted email address to the implicitly created primary key field, not the email field.

Use:

mail = NewsletterEmails(email=self.instance.user.email)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文