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 778729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:48:44+00:00 2026-05-14T19:48:44+00:00

I’m new to Django, trying to process some forms. I have this form for

  • 0

I’m new to Django, trying to process some forms.
I have this form for entering information (creating a new ad) in one template:


class Ad(models.Model):
    ...
    category = models.CharField("Category",max_length=30, choices=CATEGORIES)
    sub_category = models.CharField("Subcategory",max_length=4, choices=SUBCATEGORIES)
    location = models.CharField("Location",max_length=30, blank=True)
    title = models.CharField("Title",max_length=50)
    ...
-----------------------------------
class AdForm(forms.ModelForm):
    class Meta:
        model = Ad
    ...

I validate it with “is_valid()” and all is fine.

Basically for the second validation (another template) I want to validate only against “category” and “sub_category”:

In another template (with another method from views.py), I want to use 2 fields from the same form (“category” and “sub_category”) for filtering information – and now the “is_valid()” method would not work correctly, cause it validates the entire form, and I need to validate only 2 fields. I have tried with the following:


   ...
   if request.method == 'POST': # If a filter for data has been submitted:
       form = AdForm(request.POST)
       try:
           form = form.clean()
           category = form.category
           sub_category = form.sub_category
           latest_ads_list = Ad.objects.filter(category=category)
       except ValidationError:
           latest_ads_list = Ad.objects.all().order_by('pub_date')
   else:
       latest_ads_list = Ad.objects.all().order_by('pub_date')
       form = AdForm()
   ...

but it doesn’t work.

EDIT:
Solved it by adding:


class FilterForm(forms.ModelForm):
    class Meta:
        model = Ad
        fields = ('category', 'sub_category')

and validating this form with “is_valid()” etc., which worked just fine.

  • 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-14T19:48:45+00:00Added an answer on May 14, 2026 at 7:48 pm

    Have you tried subclassing AdForm and modifying the fields in the inner Meta class? Something like this:

    class AdFormLite(AdForm):
        class Meta:
            fields = ['category', 'sub_category']
    

    From the documentation for ModelForm on changing the order of fields:

    The fields attribute defines the
    subset of model fields that will be
    rendered, and the order in which they
    will be rendered.will be rendered.

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

Sidebar

Related Questions

No related questions found

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.