django:我如何将email_token_expiry日期时间指定为null?

时间:2012-01-19 19:18:48

标签: python django datetime django-models

def confirmemail(request,token):

    user = Users.objects.get(email_token = token)
    if user:
        user.email_token = ''
        user.email_token_expiry = ''
        user.email_verified = 1
        user.active = 1
        user.save()
    return render_to_response('confirmemail.html', {'user': user}, context_instance=RequestContext(request))

我收到错误:

ValidationError at /confirmemail/mw054b7el2/

[u'Enter a valid date/time in YYYY-MM-DD HH:MM[:ss[.uuuuuu]] format.']

Request Method:     GET
Request URL:    http://127.0.0.1:8000/confirmemail/mw054b7el2/
Django Version:     1.3.1
Exception Type:     ValidationError
Exception Value:    

[u'Enter a valid date/time in YYYY-MM-DD HH:MM[:ss[.uuuuuu]] format.']

Exception Location:     /usr/local/lib/python2.6/dist-packages/django/db/models/fields/__init__.py in to_python, line 710
Python Executable:  /usr/bin/python
Python Version:     2.6.6
Python Path:    

['/home/user1/djangoblog',
 '/usr/lib/python2.6',
 '/usr/lib/python2.6/plat-linux2',
 '/usr/lib/python2.6/lib-tk',
 '/usr/lib/python2.6/lib-old',
 '/usr/lib/python2.6/lib-dynload',
 '/usr/local/lib/python2.6/dist-packages',
 '/usr/lib/python2.6/dist-packages',
 '/usr/lib/python2.6/dist-packages/gst-0.10',
 '/usr/lib/pymodules/python2.6',
 '/usr/lib/pymodules/python2.6/gtk-2.0']

email_token_expiry是mysql表中的datetime字段。

如何将email_token_expiry设置为null或空或右侧格式化null / 0 /空日期时间?

如果有人帮助我解决这个问题,我们将不胜感激。

models.py:

class Users(models.Model):
    username = models.CharField(max_length=255,unique=True)
    slug = models.CharField(max_length=255, default='0')
    password = models.CharField(max_length=300)
    password_token = models.CharField(max_length=300, default='0')
    email = models.CharField(max_length=255, unique=True)
    email_verified = models.BooleanField(default=False)
    email_token = models.CharField(max_length=255)
    email_token_expiry = models.DateTimeField()
    tos = models.BooleanField(default=False)
    active = models.BooleanField(default=False)
    last_login = models.DateTimeField(auto_now_add=True)
    last_action = models.DateTimeField(auto_now_add=True)
    is_admin = models.BooleanField(default=False)
    role = models.CharField(max_length=255, default='0')
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now_add=True)

    def __unicode__(self):
        return self.username

class UsersModelForm(forms.ModelForm):

    def __init__(self, *args, **kwargs):
        super(UsersModelForm, self).__init__(*args, **kwargs)
        self.fields.keyOrder = ['username', 'email', 'password', 'passwordrepeat']


    passwordrepeat = forms.CharField(max_length=300, widget=forms.PasswordInput, label=u'Repeat Password')

    class Meta:
        model = Users
        fields = ('username', 'password', 'passwordrepeat', 'email')
        widgets = {
            'password' : forms.PasswordInput(),
        }

    def clean(self):
        cleaned_data = self.cleaned_data
        password = cleaned_data.get("password")
        passwordrepeat = cleaned_data.get("passwordrepeat")
        if password != passwordrepeat:
            raise forms.ValidationError("Passwords must match.")

        return cleaned_data

2 个答案:

答案 0 :(得分:2)

指定email_token_expiry为@ArthurNeves在models.py中建议。

email_token_expiry = models.DateTimeField(null=True, blank=True)

然后当你设置email_token_expiry时,它必须是:

user.email_token_expiry = None

user.email_token_expiry = ''

答案 1 :(得分:1)

我认为您需要为进一步的数据设置令牌!试试这个:

import datetime
user.email_token_expiry = datetime.datetime.now()+datetime.timedelta(hours=24)

修改

或者如果要将数据设置为空| null,则只需修改模型字段:

email_token_expiry = models.DateTimeField(blank=True, null=True)

这将允许你设置为无或空白

更多文档:https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.Field.null

还有一个想法,在您更改模型后,如果您使用南方,请不要忘记应用迁移,或者如果您没有迁移工具集,请删除表并再次创建它!

<强> EDIT2 确保删除表用户并立即运行syncdb。 (如果你没有南方设置) 之后,您可以检查您的数据库,看看该字段是否可以为空!在您的数据库中发出'desc ..users',这样您就可以仔细检查一下。 如果仍然无法正常工作