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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:03:20+00:00 2026-06-04T11:03:20+00:00

I’d like to know how I can design my Django model to achieve the

  • 0

I’d like to know how I can design my Django model to achieve the following:

 Road -> Category (required): Highway (select list)

 Road -> Attribute (optional): Traffic -> Heavy + Moderate (checkboxes)

 Road -> Attribute (optional): Condition -> Smooth + Rough + Average(checkboxes)

Does it make sense to include TRAFFIC_CHOICES, CONDITION_CHOICES under the Road class vs creating separate classes for each set of choices vs creating a generic Attribute class?

How do I display choices as checkboxes?

The end goal of this model is to be able to create queries such as “Highway roads that are smooth with no traffic“

Here is my attempt:

class Category(models.Model):

    CATEGORY_CHOICES = (
        ('highway', 'Highway'),
        ('parkway', 'Parkway'),
    )
    name = models.CharField(max_length=1, choices=CATEGORY_CHOICES, blank=False)

class Road(models.Model):

        name = models.TextField(blank=False)

        TRAFFIC_CHOICES = (
            ('moderate', 'Moderate'),
            ('busy', 'Busy'),
        )
        traffic = models.CharField(max_length=1, choices=TRAFFIC_CHOICES)

        CONDITION_CHOICES = (
            ('smooth', 'Smooth'),
            ('rough', 'Rough'),
            ('average', 'Average'),
        )
        condition = models.CharField(max_length=1, choices=CONDITION_CHOICES)
  • 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-06-04T11:03:22+00:00Added an answer on June 4, 2026 at 11:03 am

    First, change the first models.TextField to a CharField like the others.

    Category does not have to be a separate model, unless you intend to add new categories after your application is finished, in which case it must be a separate model and you should use a ForeignKey relationship from the Road to the Category and ditch the CATEGORY_CHOICES.

    Assuming you do not ever intend to add new categories, you can get rid of the Category model entirely and put the CATEGORY_CHOICES into Road. Then change name = to category = and put that into Road too.

    You have a max_length of 1 for all of those fields, which is fine, but in that case you need to make the CHOICES map to single characters so they fit in the field. For instance:

    CATEGORY_CHOICES = (
        ('H', 'Highway'),
        ('P', 'Parkway'),
    )
    

    Why do you want to use checkboxes for your traffic and condition choices? Checkboxes mean that you can select multiple answers instead of being forced to select just one. This doesn’t make sense conceptually for road condition or traffic, and it isn’t compatible with a CharField (because a CharField can only store one value, without some very contrived setup). You could keep the system you have and use radio buttons if you prefer them over dropdowns, but you can’t use checkboxes without getting rid of the choices and instead making each possible checked box its own BooleanField or NullBooleanField.

    I usually put my choices outside of the class definition. I don’t know if it works how you expect it to work when it’s inside. I’m going to move them outside the class definition for my example; this may not be required, so feel free to experiment.

    To summarize (and I’m changing the name field to a CharField because TextField is only for really large blocks and not for names):

    CATEGORY_CHOICES = (
        ('H', 'Highway'),
        ('P', 'Parkway'),
    )
    TRAFFIC_CHOICES = (
        ('M', 'Moderate'),
        ('B', 'Busy'),
    )
    CONDITION_CHOICES = (
        ('S', 'Smooth'),
        ('R', 'Rough'),
        ('V', 'Varying'),
    )
    
    class Road(models.Model):
        name = models.CharField(max_length=512, blank=False)
        category = models.CharField(max_length=1, choices=CATEGORY_CHOICES, blank=False)
        traffic = models.CharField(max_length=1, choices=TRAFFIC_CHOICES)
        condition = models.CharField(max_length=1, choices=CONDITION_CHOICES)
    

    Edit: If you really are sure that checkboxes are the best fit, then you have two main choices. As above, if you intend to add choices later after your application is finished, then your strategy is different (you need to use a ManyToManyField). Otherwise you could implement them like this:

    class Road(models.Model):
        name = models.CharField(max_length=512, blank=False)
        category = models.CharField(max_length=1, choices=CATEGORY_CHOICES, blank=False)
        moderate_traffic = models.NullBooleanField()
        heavy_traffic = models.NullBooleanField()
        smooth_condition = models.NullBooleanField()
        rough_condition = models.NullBooleanField()
        varying_condition = models.NullBooleanField()
    

    The part where you display them as grouped checkboxes specifically happens when you’re displaying your form, not in the model.

    You could also use some kind of bit field, but that’s not included in Django by default — you’d have to install an extension.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Does anyone know how can I replace this 2 symbol below from the string
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I would like to count the length of a string with PHP. The string
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.