django models choices example

Will create a select box for this in the model form. Use can select from the
options.The first element in each tuple is the actual value to be set on the model, 
and the second element is the human-readable name. For example:
YEAR_IN_SCHOOL_CHOICES = [
        (FRESHMAN, 'Freshman'),
        (SOPHOMORE, 'Sophomore'),
        (JUNIOR, 'Junior'),
        (SENIOR, 'Senior'),
        (GRADUATE, 'Graduate'),
    ]
    year_in_school = models.CharField(
        max_length=2,
        choices=YEAR_IN_SCHOOL_CHOICES,
        default=FRESHMAN,
    )

4.25
4
Phoenix Logan 186120 points

                                    from django.db import models

class Author(models.Model):
   first_name = models.CharField(max_length=64)
   last_name = models.CharField(max_length=64)

   def __str__(self):
       return '{} {}'.format(self.first_name, self.last_name)


class Book(models.Model):
   author = models.ForeignKey('books.Author', related_name='books', on_delete=models.CASCADE)
   title = models.CharField(max_length=128)
   status = models.CharField(
       max_length=32,
       choices=[],  # some list of choices
   )

   def __str__(self):
       return '{}: {}'.format(self.author, self.title)

4.25 (4 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
choices in model django choices field in django models django model fileds choices displaying choices field iin django django creating choices in model choices django models field choice django django field choices forms django model choices query view model choices in django template django get choices from model field what to put inside the choices django model field django field choices choices django model django cahr field choices django models field with choices djangp field with choices django models.choicese _CHOICES DJANGO model.choices in django what is choices in django choices field design django form django choices form how to get the choices of django model django how to get the choices from model django get model field choices django choices from other model get choices django choices field in django how to add models choices on django django model defining choices how does model choices work django using choices in django models from choices django choices field django form django models choice django choices= django field choices form choices model django choices using forms django choices in forms django choices in django model django form choices how to use choices in django models how to model choices django models choice field django models.choices choices django example django model choices function write choices in django models django choices value django choices field from queryset what does _ means in choices of models in django django choice field choices django choices in model django choices in model4 model choices for django model model.choices django django choices field model choices field in django template django choices object django models how choices work choices in django django choices model choices field django do you need to put choices inside the model django field.choices django python django model choices django choices field django choices model field models.choices django Use of django choices get choices from model django giving choices in django model how to use obtain value from choices models in django choices model field django choices in models django django models.Choices How to put choices in django models django model choices field choice fields django models choices django django form choices from model django get choices from model choices in django models choices for django model field models django choices django get model object choices django models choices example django models choices giving choices in django models django model choices django create model choices how to access a model choices django django choices
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source