Description of the problem
Taxonomy is the science of defining and naming groups of biological organisms on the basis of shared characteristics. Organisms are grouped together into taxa (singular: taxon) and these groups are given a taxonomic rank. The principal ranks in modern use are domain, kingdom, phylum, class, order, family, genus and species. More information on Taxonomy and Taxonomic ranks in Wikipedia.
Following the example for the red fox in the article Taxonomic rank in Wikipedia I need to create a JSON output like this:
{
"species": "vulpes",
"genus": "Vulpes",
"family": "Canidae",
"order": "Carnivora",
"class": "Mammalia",
"phylum": "Chordata",
"kingdom": "Animalia",
"domain": "Eukarya"
}
Since Django REST Framework creates the keys based on the field names, the problem arises with the taxonomic rank class (bold in the example) as it is a reserved word in Python and can't be used as a variable name.
What I have tried
A model class created in Django would look like this:
class Species(models.Model):
species = models.CharField()
genus = models.CharField()
family = models.CharField()
# class = models.CharField() - class is reserved word in Python
# class_ = models.CharField() - Django doesn't allow field names
# ending with underscore. That wouldn't be either a satisfying solution.
# further fields
Question
Is there any possible way to solve this problem and generate the desired output? If not, what is the best practice to work around this problem?