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

  • Home
  • SEARCH
  • 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 652915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:19:29+00:00 2026-05-13T22:19:29+00:00

I need to add extra validation to my DateField in Admin to make sure

  • 0

I need to add extra validation to my DateField in Admin to make sure the date given is in the future. I have no experience in such a thing, so here’s what I’ve done.
1) I’ve created custom form field and added validation to it:

class PastDateField(forms.DateField):
    def clean(self, value):
    """Validates if only date is in the past
    """
        if not value:
            raise forms.ValidationError('Plase enter the date')
        if value > datetime.now():
            raise forms.ValidationError('The date should be in the past, not in future')
        return value

2) Then I’ve added custom model form:

class CustomNewsItemAdminForm(forms.ModelForm):
    title = forms.CharField(max_length=100)
    body = forms.CharField(widget=forms.Textarea)
    date = PastDateField()
    region = forms.ModelChoiceField(Region.objects)

3) And here’s how I’ve registered admin:

class NewsItemAdmin(admin.ModelAdmin):
    form = CustomNewsItemAdminForm

    def queryset(self, request):
        return NewsItem.objects.all()

admin.site.register(NewsItem, NewsItemAdmin)

The result of this is that my admin form
1) Shows field I haven’t specified in custom admin form
2) Lacks JavaScript calendar for the datetime field

It’s pretty obvious to me that I’m doing something wrong, but I’ve found no examples relevant to my needs as I am a noob. What is the better way to add custom validation to datetime field without messing things up?

EDIT: Thanks a lot to Brian Luft and Daniel Roseman for correct answers! To make this post helpful for someone facing the same problem here is the resulting code:

class CustomNewsItemAdminForm(forms.ModelForm):
    class Meta:
        model = NewsItem

    def clean_date(self):
        """Validates if only date is in the past
        """
        date = self.cleaned_data["date"]
        if date is None:
            raise forms.ValidationError('Plase enter the date')
        if date > datetime.now().date():
            raise forms.ValidationError('The date should be in the past, not in future')
        return self.cleaned_data["date"]

class NewsItemAdmin(admin.ModelAdmin):
    form = CustomNewsItemAdminForm

    def queryset(self, request):
        return NewsItem.objects.all()

admin.site.register(NewsItem, NewsItemAdmin)
  • 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-13T22:19:30+00:00Added an answer on May 13, 2026 at 10:19 pm

    Firstly, declaring fields explicitly on a ModelForm – whether in or out of the admin – does not mean that the other fields will not be displayed. You need to define the fields or exclude tuples in the form’s inner Meta class. If the other fields are all the default, you can simply declare the one you are overriding.

    Secondly, if you want your custom field to use the javascript, you’ll need to use the right widget, which is django.contrib.admin.widgets.AdminDateWidget. However, there is a much easier way to do this, which is not define a custom field at all, but instead define a clean_date method on the form itself.

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

Sidebar

Related Questions

I use a jquery validation plugin and I need to add some extra checking,
I'm trying to add extra validation to jdpl process-definition files using XSD. We have
I need to use the org.osgi.framework.system.packages.extra property to add service interfaces at runtime. These
Hї! I have wrote test for my application. I need add item to database
Need to add two same name .csproj class libraries in my solution.Have two project
I need to add some extra field to Joomla registration form without using any
I can't use wx.ProgressDialog because I need to add extra contents to the dialog
Why do I need to do add extra namespace declarations (below) in a Flex
I need to add extra fields in the CSR, like keyusage, regestrationID etc.I am
im new to python gasp . im wondering do i need extra add-on module

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.