1

In my Django models.py file, I have a variable ColourChoices = ((0, "GREEN"), (1, "YELLOW"), (2, "RED"), (3, "GREY")) where in the same file, ColourChoices is used in class

class Rainbow(models.Model):
    colour = models.IntegerField(choices=ColourChoices)
      

I was wondering if there is a viable way to create a view for ColourChoices (i.e. some JSON endpoint), such that you can get all the existing values for it (GREEN, YELLOW, RED, GREY) without creating a new table in the underlying database and adding those values to it? That is, in such a way that if I were to add a new colour (e.g. BLUE), or remove one colour from the choices, that doing it once in the code would do the trick (i.e. change what you see in the view).

  • You mean , You want to do it in `Frontend` ? – Lars Feb 15 '21 at 04:21
  • Refer this answer : https://stackoverflow.com/a/54409752/1079086 you need to create one serializer with choice field and create view for the same. then you can easily get list of choices in frontend as well. – Akash senta Feb 15 '21 at 05:24

0 Answers0