I’m using enumeration types,
TextChoices to be precise, to enumerate possible and valid options for attribute in a model.
Later I use Django Admin to create new objects.
How can I show only subset of this enumerated options in a select input ?
For example, I have this code in
class LanguageChoice(models.TextChoices): EN = "EN", "English" DE = "DE", "German" IT = "IT", "Italian" class Article(models.Model): title = models.CharField(max_length=100) language = models.CharField( max_length=2, choices=LanguageChoice.choices, default=LanguageChoice.EN )
Then in admin.py I have:
class ArticleAdmin(admin.ModelAdmin): fields = ['title','language'] admin.site.register(Article, ArticleAdmin)
Let’s say I want to achieve that user is able to choose (when creating new Article) only between EN and DE.
But option IT is still valid option to be saved in database.
How to achieve that ?