How can I fix this error "premium is not one of valid choices please select valid choice" from drop down in django?

Viewed 77

Context:

I was able to set a user to PREMIUM from the admin panel but after resetting the database I see this error. I'm unable to figure out what's going on or how to fix it.

The error is: "Select a valid choice. PREMIUM is not one of the available choices".

Here is code for my Profile class:

  class Profile(models.Model): 
        PREMIUM_CHOICES = (("FREE",0),("PREMIUM",1))
        user = models.OneToOneField(User,primary_key=True,on_delete=models.CASCADE)
        premium = models.IntegerField(choices=PREMIUM_CHOICES,default=0)
        due_date = models.DateField(null=True)

I've looked at several answers on Stackoverflow: MongoDB database. Error "Select a valid choice. That choice is not one of the available choices."

Here is screen capture of the error:

enter image description here

1 Answers

As the documentation on the choices=… parameter [Django-doc] says:

A sequence consisting itself of iterables of exactly two items (e.g. [(A, B), (A, B) …]) to use as choices for this field. If choices are given, they’re enforced by model validation and the default form widget will be a select box with these choices instead of the standard text field.

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.

The first item is thus the key (here an int) and the second a human-readable name of that value. The choices thus should be:

PREMIUM_CHOICES = ((0, 'FREE'),(1, 'PREMIUM'))

Note: It is normally better to make use of the settings.AUTH_USER_MODEL [Django-doc] to refer to the user model, than to use the User model [Django-doc] directly. For more information you can see the referencing the User model section of the documentation.

Related