Getting Choices for CharField from external API


I am working with data that I get from an external source. One field is not easily converted to something user readable. The data I get from the API is “encoded”. It returns as ##user00, ##user01, ##user02 etc.

But there is an API endpoint that returns the translation table. This table may change or at least be updated.

Can I get the CharField with Choices behavior but not enforce the data validation and update the choices without a migration?

Category field below is stored as ##user00, ##user01 , ##user02 etc but I want it to be showed to the user as something else.

class Product(models.Model):
    objects = ProductQuerySet.as_manager()
    product_url = models.CharField(max_length=200, unique=True)
    product_id = models.CharField(max_length=200, null=True, blank=True)
    description = models.CharField(max_length=200, null=True, blank=True)
    category = models.CharField(max_length=200, null=True, blank=True)

Yes, there are a couple different ways to handle this.

Probably the cleanest and easiest way would be to store those translation table entries in a separate model. You can then use the ModelChoiceField to use that table as your choices and validators.

Thank you, but I can only find ModelChoiceField as part of a form. Not as a model field. I primarily want this for displaying data. In the Model field reference I can only find Choices as part of other fields.

You would also be able to use that table for any purpose. You would not be restricted to using that model only for the ModelChoiceField. It becomes a general translation table that you can use anywhere.

What I am thinking now, to get filters to work and data display to show a friendly name, is that I create a new model with the translation. Have a one-to-many field in the Product model that I connect to the new model to get filters etc to hopefully work kind of out of the box.

If I would need to push info about the product back to the API it would make it a bit trickier but at the moment it is one way.