[Answered ]-Django models.IntegerField choices

1👍

Your choice is validated by the model, and since that only has 0 as option, it is rejected.

But it is not a good idea to define choices which are derived from (another) model. You can work with a ForeignKey [Django-doc] instead:

class StatusBase(models.Model):
    On = "On"
    Off = "Off"
    Status_CHOICES = [
        (On, 'On'),
        (Off, 'Off'),
    ]
    status = models.CharField(max_length=10, choices=Status_CHOICES, default=On)

    class Meta:
        abstract = True


class Person(StatusBase, models.Model):
    type = models.CharField(max_length=127)
    # …


class Class(StatusBase, models.Model):
    name = models.CharField(max_length=255, verbose_name='Class name')
    teacher = models.ForeignKey(
        Person, limit_choices_to={'type': 'Teacher', 'status': 'On'}
    )

Note: Models in Django are written in PascalCase, not snake_case,
so you might want to rename the model from class_info to Class.


Note: Models normally have no …Info suffix. Therefore it might be better to rename person_info to Person.


Note: normally the name of the fields in a Django model are written in snake_case, not PascalCase, so it should be: name instead of Name.


Note: Normally model fields have no prefix with the name of the model. This makes
queries longer to read, and often want uses inheritance of (abstract) models to
inherit fields, so using a prefix would make it less reusable. Therefore it
might be better to rename your field class_name to name.

Leave a comment