验证Django中电子邮件的ArrayField的每个值

时间:2018-11-28 07:11:29

标签: django postgresql validation django-models

我将PostgreSQL与Django结合使用,并尝试使用ArrayField(models.EmailField(max_length=200))。但是当提供无效的电子邮件地址时,我最终得到的是500 error code(TypeError)

如何验证ArrayField中的每封电子邮件。我试过在序列化器和模型中都编写validate方法。但是没有任何帮助。验证ArrayFields的正确方法是什么?我正在使用Django 2.1和Python 3.6

错误的追溯如下...请看

models.py

billing_emails = ArrayField(models.EmailField(max_length = 200))

company = models.OneToOneField(Company,on_delete = models.CASCADE)

追踪

Traceback (most recent call last):
  File "/home/user/.local/share/virtualenvs/backend-Amy45Zrz/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/user/.local/share/virtualenvs/backend-Amy45Zrz/lib/python3.6/site-packages/django/core/handlers/base.py", line 156, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/user/.local/share/virtualenvs/backend-Amy45Zrz/lib/python3.6/site-packages/django/core/handlers/base.py", line 154, in _get_response
    response = response.render()
  File "/home/user/.local/share/virtualenvs/backend-Amy45Zrz/lib/python3.6/site-packages/django/template/response.py", line 106, in render
    self.content = self.rendered_content
  File "/home/user/.local/share/virtualenvs/backend-Amy45Zrz/lib/python3.6/site-packages/rest_framework/response.py", line 72, in rendered_content
    ret = renderer.render(self.data, accepted_media_type, context)
  File "/home/user/.local/share/virtualenvs/backend-Amy45Zrz/lib/python3.6/site-packages/djangorestframework_camel_case/render.py", line 8, in render
    return super(CamelCaseJSONRenderer, self).render(camelize(data), *args,
  File "/home/user/.local/share/virtualenvs/backend-Amy45Zrz/lib/python3.6/site-packages/djangorestframework_camel_case/util.py", line 17, in camelize
    new_dict[new_key] = camelize(value)
  File "/home/user/.local/share/virtualenvs/backend-Amy45Zrz/lib/python3.6/site-packages/djangorestframework_camel_case/util.py", line 16, in camelize
    new_key = re.sub(r"[a-z]_[a-z]", underscoreToCamel, key)
  File "/home/user/.local/share/virtualenvs/backend-Amy45Zrz/lib/python3.6/re.py", line 191, in sub
    return _compile(pattern, flags).sub(repl, string, count)
TypeError: expected string or bytes-like object

谢谢。

1 个答案:

答案 0 :(得分:0)

如果使用的是表单,请使用FormSerializer,可以使用$words = explode(' ',$this->globalSearch); if (count($words) > 1) { foreach ($words as $word) { $query->orFilterWhere(['like', 'fullname', $word]); } } else { $query->orFilterWhere(['like', 'fullname', $this->globalSearch]); } 方法来验证数据。例如:

clean_<attribute_name>

您也可以使用model's clean method

from django.core.validators import email_re
from django.core.exceptions import ValidationError

class YourForm(forms.ModelForm):  # serializers.ModelSerializer

    def clean_billing_emails(self): 
         data = self.cleaned_data['billing_emails']
         for email in data:
             if not email_re.match(email):
                 raise ValidationError('Invalid email {}'.format(email)  # or raise serializers.ValidationError
         return data