【发布时间】:2013-12-09 22:47:26
【问题描述】:
我是 Python/Django 的新手,我正在尝试将电话号码插入模型中的字段。
我的 models.py 中的代码是
from django.db import models
from django.core.validators import *
telephone = RegexValidator(r'^\d+$', 'Only numeric characters are allowed.')
class Registration(models.Model):
mobile_telephone = models.PositiveIntegerField(max_length=18, validators=[telephone, MaxLengthValidator])
forms.py 的代码
from django.forms import ModelForm
from django import forms
from registration.models import Registration
class RegistrationForm(ModelForm):
mobile_telephone = forms.CharField(max_length=18,
error_messages={'invalid':'Enter a valid mobile number',
'required':'Enter a valid mobile number'}
)
我已经尝试了带有 IntegerField、CharField 和 PositiveIntegerField 的 models.py,但它们都不允许在字段的开头使用 0。
我做错了什么?
谢谢
【问题讨论】:
-
McAbra - 错误是它不接受以 0 开头的数字。即 - 我在表单字段中输入 0123456789,它被保存到数据库中,因为 123456789 缺少 0。作为电话号码0 很重要。