Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 843797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:11:12+00:00 2026-05-15T06:11:12+00:00

NEWBIE ALERT! background: For the first time, I am writing a model that needs

  • 0

NEWBIE ALERT!

background:

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).

My question:

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

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    
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T06:11:13+00:00Added an answer on May 15, 2026 at 6:11 am

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

    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 
    

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Newbie alert! OK, I have a working central Mercurial repository that I've been working
ruby newbie alert! (hey that rhymes :)) I have read the official definition but
NEWBIE ALERT! Here's the situation. I've got an Android ListActivity class (AppWindow) that contains
(Newbie DBUnit question Alert!) It appears that DBUnit for each table 'deletes all the
.NET newbie alert Using Visual C# 2008 Express Edition I have accidentally created a
I am a jquery newbie and have been adding alert timestamps into my html
First: I'm JavaScript newbie. So.. I have basic form with password, repeat password, email
WARNING: Complete newbie to RoR and Ruby alert! * I have a login method
* Newbie Alert * I have an AdvancedDataGrid with 5 columns. The second column,
Newbie alert... I'm trying to make an educational that uses different audio players (with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.