'choices' must be a mapping (e.g. a dictionary) or an iterable (e.g. a list or tuple)

Hello!

I have added a few new fields to my model called Profile and get an error when trying to create migrations.

Have anyone else encountered similar error? What causes it and how did you solve it?

Model:

class Profile(models.Model):
    member = models.OneToOneField(Member, on_delete=models.CASCADE)
    hetero_man_hair_color = models.CharField(max_length=100, blank=True, choices=hair_color)
    hetero_man_eye_color = models.CharField(max_length=10, blank=True, choices=eye_color)
    hetero_man_ethnicity = models.CharField(max_length=50, blank=True, choices=ethnicity)

    last_login_from_country = models.CharField(max_length=100, default="")
    last_login_from_city = models.CharField(max_length=100, default="")
    hit_count = models.PositiveIntegerField(default=0)
    profile_likes = models.IntegerField(default=0)
    
    class Meta:
        verbose_name = "Användarprofil"
        verbose_name_plural = "Användarprofiler"

    def __str__(self):
        return str(self.member)

    def get_absolute_url(self):
        return reverse("profilepage_app:profile-redirect", args=[self.member.username_slug])

Error:

profile_app.Profile.hetero_man_hair_color: (fields.E004) 'choices' must be a mapping (e.g. a dictionary) or an iterable (e.g. a list or tuple).
profile_app.Profile.hetero_man_eye_color: (fields.E004) 'choices' must be a mapping (e.g. a dictionary) or an iterable (e.g. a list or tuple).
profile_app.Profile.hetero_man_ethnicity: (fields.E004) 'choices' must be a mapping (e.g. a dictionary) or an iterable (e.g. a list or tuple).
hair_color = (("Blond", "Blond"), ("Svart", "Svart"), ("Brun", "Brun"), ("Röd", "Röd"), ("Annat", "Annat"))
eye_color = (("Gröna", "Gröna"), ("Blå", "Blå"), ("Brun", "Brun"), ("Grå", "Grå"), ("Nötbrun", "Nötbrun"), ("Röda", "Röda"), ("Annat", "Annat"))
ethnicity = (("Europisk", "Europisk"), ("Asiat", "Asiat"), ("Afrikan", "Afrikan"), ("Nordamerikank", "Nordamerikank"), ("Latino", "Latino"), ("Annat", "Annat"),)

What I can see I am using nested tuple, which is an iterable and it did work prior adding the new fields.

All help is appriciated.

Physically within the code, where are these variables defined relative to this model?

Hello Mr ken!

I have the variables stored in this folder: project_folder → general → common_objects.py. I’m importing the variables to several apps and models.

I have other variables. similar to the ones I shared, in the same folder and they don’t cause any error.

Hello!

Your comment made me think about the placement of the variables. I moved the variables to the top within the model and now everything works as it should. My Test codes causes no error as well.

I don’t understand why it worked because I had other fields with the same dependencies but somehow it works.

Thank you sir!