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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:50:50+00:00 2026-05-23T00:50:50+00:00

I am creating an application where users can set objects and the corresponding data

  • 0

I am creating an application where users can set objects and the corresponding data they want to record. Let me give you an example:

class vehicle_type(models.Model):
    name = models.CharField(max_length=20, unique=True)
    int1_name = models.CharField(max_length=20, blank=True, null=True)
    int1_default = models.IntegerField(blank=True, null=True)
    int2_name = models.CharField(max_length=20, blank=True, null=True)
    int2_default = models.IntegerField(blank=True, null=True)
    float1_name = models.CharField(max_length=20, blank=True, null=True)
    float1_default = models.FloatField(blank=True, null=True)

class vehicle(models.Model):
    registration = models.CharField(max_length=20)
    vehicle_type = models.ForeignKey(vehicle_type)
    int1_val = models.IntegerField(blank=True, null=True)
    int2_val = models.IntegerField(blank=True, null=True)
    float1_val = models.FloatField(blank=True, null=True)

Where the data would be something like this to describe the vehicles:

    # pseudo code
    vehicle_type('Car','Seats',4,'Doors',4,'',)
    vehicle_type('Van','Seats',2,'',,'Load',3.2)

and then the data on the vehicles would be:

    vehicle('ABC 123',1,2,2,) #sports car
    vehicle('DEF 456',1,6,,) #SUV
    vehicle('GHI 789',2,,,1.2) #light van
    vehicle('JKL 246',2,4,3,3.6) #heavy van

My question is how can I make sure that the vehicleForm does not display the fields NOT required by the vehicle_type? I know I could pass an instance of the vehicle_type to the vehicleForm template and only display elements of the vehicleForm if they are declared in the vehicle_type, but this seems unnecessary and moves logic into the template. (I hope all this makes sense)

  • 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-23T00:50:51+00:00Added an answer on May 23, 2026 at 12:50 am

    You can pass vehicle_type to VehicleForm.__init__(), and put there some custom logic which would, for example, set widgets of fields that are not required to HiddenInput.

    Basic example (code is not tested)::

    class VehicleForm(forms.ModelForm):
        def __init__(self, vehicle_type, *args, **kwargs):
            if vehicle_type.name == 'Car':
                self.fields[some_field_for_trucks].widget = forms.HiddenInput()
            super(VehicleForm, self).__init__(*args, **kwargs)
    

    And when you instantiate VehicleForm in you view, you just pass as a first argument your the vehicle_type selected by user.

    See examples of dynamic forms:

    • dynamically add field to a form
    • Django dynamic Form example
    • http://snippets.dzone.com/posts/show/7936 (includes setting widget attributes)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're planning to create a web application where users can build custom forms, choosing
I am creating event search application, we set search criteria from one screen populate
I'm writing an application that displays a list of objects which the user can
I have been task with (ha) creating an application that will allow the users
I'm creating a web application for work where the user has to enter the
When creating a web application, and lets say you have a User object denoting
I am creating an ASP script that uses the application object to store the
When I'm creating a user for my web application, an SMTP email (using ASP.NET's
I am creating a basic image browsing application using Silverlight. Depending on the user's
For a web application, when creating the user which will connect to the MySQL

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.