开发者

(Django) saving string to EmailField raises 'invalid literal for int() with base 10'

开发者 https://www.devze.com 2023-01-06 15:11 出处:网络
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 :

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.p开发者_如何学JAVAy" 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: 'foo@bar.com'

My NewsletterEmail model :

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

How to get around this problem ?


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)
0

精彩评论

暂无评论...
验证码 换一张
取 消