#StackBounty: #django #django-models Set attribute based on another attribute

Bounty: 50

How to set hostel choices based on gender?

Global variables

BOYS_HOSTEL_CHOICES = (…)

GIRLS_HOSTEL_CHOICES = (…)

class Student(models.Model):

    MALE = 'M'
    FEMALE = 'F'
    
    #...
    GENDER_CHOICES = (
        (MALE, 'Male'),
        (FEMALE, 'Female')
    )

    gender = models.CharField(
        max_length = 10,
        choices = GENDER_CHOICES,
        verbose_name="gender",
    )


    hostel = models.CharField(
        max_length=40,
        choices = BOYS_HOSTEL_CHOICES if gender == 'M' # <---
                            else GIRLS_HOSTEL_CHOICES,
        default = 'some hostel'
    )


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.