English 中文(简体)
Django模型验证在壳体内不发生。
原标题:Django model validation does not occur in the shell

I have an ostensibly simple problem. Here is my models.py:

from django.db import models

class Email(models.Model):
    def __unicode__(self):
       return self.email
    email = models.EmailField()

EmailField, according to the docs, should check if the entered email address is valid. So then why does my shell allow me to save malformed emails? In the shell:

>>> from emailapp.models import Email
>>> e = Email(email="sdf")
>>> e
<Email: sdf>
>>> e.save()
>>> Email.objects.all()
[<Email: [email protected]>, <Email: [email protected]>, <Email: sdf>]

The admin interface doesn t allow me to save these malformed emails, but the shell does.Why is this?

最佳回答

文件明确显示,,必须称呼核证

问题回答

一些外地有效人员只能以表格形式(django admin使用表格)。 在这种情形下,<代码>Email Field刚刚被翻译成Char Field(输入数据库),因此,你没有从空壳中发现任何错误。

我认为,之所以这样做,是因为制定模型并非非常常见。 如果你仍然希望从任何地方确认正确的电子邮件,我建议你超越<代码>save(方法:

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

def save(self, *args, **kwargs):
    if not email_re.match(self.email):
        raise  ValidationError(u %s is not a correct email  % self.email)
    super(Email, self).save(*args, **kwargs)




相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签