Django:管理员中的验证错误 [英] Django: Validation error in Admin

查看:87
本文介绍了Django:管理员中的验证错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NEWBIE ALERT!

背景:

我正在写一个需要验证的模型。我不能拥有重叠日期范围的两个项目。我有一切工作,除了当我提出forms.ValidationError,我得到黄色的屏幕死亡(debug = true)或500页(debug = false)。

For the first time, I am writing a model that needs to be validated. I cannot have two Items that have overlapping "date ranges". I have everything working, except when I raise forms.ValidationError, I get the yellow screen of death (debug=true) or a 500 page (debug=false).

我的问题:

如何在管理中显示错误消息一个必填的空白)?

How can I have an error message show up in the Admin (like when you leave a required filed blank)?

对于我的经验不好,请让我知道,如果我可以更好地澄清问题。

Sorry for my inexperience, please let me know if I can clarify the question better.

Models.py

Models.py

from django.db import models
from django import forms
from django.forms import ModelForm
from django.db.models import Q 

class Item(models.Model):
    name = models.CharField(max_length=500)
    slug = models.SlugField(unique=True)
    startDate = models.DateField("Start Date", unique="true")
    endDate = models.DateField("End Date")

    def save(self, *args, **kwargs):
        try:
            Item.objects.get(Q(startDate__range=(self.startDate,self.endDate))|Q(endDate__range=(self.startDate,self.endDate))|Q(startDate__lt=self.startDate,endDate__gt=self.endDate))

            #check for validation, which may raise an Item.DoesNotExist error, excepted below
            #if the validation fails, raise this error:

            raise forms.ValidationError('Someone has already got that date, or somesuch error message')

         except Item.DoesNotExist:
             super(Item,self).save(*args,**kwargs)          


    def __unicode__(self):
        return self.name

    def get_absolute_url(self):
        return "/adtest/%s/" % self.slug    


推荐答案

对于Django 1.2,请参阅 http://docs.djangoproject.com/en/dev/ref/forms/validation/#using-validation-in -practice

For Django 1.2 see http://docs.djangoproject.com/en/dev/ref/forms/validation/#using-validation-in-practice.

在1.2之前的版本中,您必须为您的管理员制作自己的模型表单,并将验证方法放在那里! http://docs.djangoproject .com / en / dev / ref / forms / validation /#cleaning-a-specific-field-attribute

In versions prior to 1.2 you would have to make your own model form for your admin and put your validation methods there! http://docs.djangoproject.com/en/dev/ref/forms/validation/#cleaning-a-specific-field-attribute

from django import forms
from models import Item

class ItemForm(forms.ModelForm):

   class Meta:
       model = Item

   def clean(self, value):
       data = self.cleaned_data
       start = data['startDate']
       end =  data['endDate']
       try:
           item = Item.objects.get(Q(startDate__range=(start,end))|\
                                   Q(endDate__range=(start,end))|\
                                   Q(startDate__lt=start,endDate__gt=end))
           raise forms.ValidationError('.....')
       except:
           pass

   return data 

然后放入你的管理员 form = ItemForm ,并确保在之前定义表单!
有关更详细的说明,请参见 http://www.jroller.com/RickHigh/ entry / django_admin_validation_of_multiple

进一步分类到django约定,你应该命名你的字段,例如。 end_date 而不是 endDate 。猜猜你甚至不需要再指定他们的verbose_name了!

Then put in your admin form=ItemForm and make sure to define the form somewhere before! For a more detailled description see http://www.jroller.com/RickHigh/entry/django_admin_validation_of_multiple.
Further to assort to django conventions you should name your fields eg. end_date and not endDate. Guess you will not even need to specify their verbose_name then anymore!

这篇关于Django:管理员中的验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆