What to do if value is different than all the defined choices for Django field choices?

I am currently working on a Django web application. I have defined a model as follows:

class MyModel(models.Model):

    fav_subject_choices=[
        ('physics', 'Physics'),
        ('chem', 'Chemistry'),
        ('math', 'Maths'),
    ]

    user = models.ForeignKey(User, on_delete=models.CASCADE)
    student_name = models.CharField(max_length=100)
    fav_subject = models.CharField(max_length=50, choices=fav_subject_choices, default='physics')

Here, I want to provide a dropdown of choices so that Admin can select favorite subject for students. For which I have defined choices and provided the same inside the field. However, the data of students is coming from an API which may have favorite subjects of students different from the choices I have defined, for example, a student can have 'English' as his/her favorite subject.

This is fine as I don't want to change the data which is already there, instead I want to add favorite subject only for students whose data for favorite subject is missing. How can it be done? Is there an option where a field can have a value other than the choices defined?

PS. This is just a dummy scenario which I have created. But it's exactly the same as the problem I am facing.

Please Create Another Model For Your Subject Choices. So Data from Other API will store in that Subject Model. Then you may use the Model as a foreign key. So you will get all new choices and also older choices.

class FavoriteSubjectChoices(models.Model):
     
    subject_name = models.CharField(max_length=100,null=true,blank=True,unique=True)

   def __str__(self):
       return self.subject_name


class MyModel(models.Model):

    
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    student_name = models.CharField(max_length=100)
    fav_subject = models.ForeignKey(FavoriteSubjectChoices, on_delete=models.CASCADE)
Back to Top