I'm currently in the process of refactoring a Django app to use Django REST Framework. Within the existing code there are 15 models for lookup tables. The structure of each model is as follows (using gender as an example - there are valid reasons in the context of the application for including a gender model rather than as hardcoded
CHOICES on a Person model):
class Gender(models.Model): """ This model stores genders """ name = models.CharField(max_length=64) sort = models.IntegerField() class Meta: ordering = ['sort'] def __unicode__(self): return self.name
(I've already made some changes that are included in the above code to bring it in line with Django's coding style guidelines)
All of the 15 models use the same field names that I am concerned should be more explicit.
My concern with using the
name field-name is that it does not explicitly state what data is in the relevant table.
To add to this, I might have the following code in my views:
class PersonByGenderList(generics.ListAPIView): """ This endpoint lists all people by gender """ serializer_class = PersonSerializer def get_queryset(self): """ :return: queryset of people restricted by gender. """ queryset = Person.objects.all() gender = self.request.QUERY_PARAMS.get('gender', None) if gender is not None: queryset = queryset.filter(gender__name=gender) return queryset
gender__name (for me) is not explicit enough. Also, in a
SlugRelatedField the slug name is always
So my questions are:
Are there any database design conventions that recommend using
nameas a generic field name on models, or should I be more explicit (following the Zen of Python) and prefer (albeit with some repetition):
queryset = queryset.filter(gender__gender=gender)
Or is it preferable to use
nameand be explicit with the model name in the case of gender?
Is the arbitrary integer field used for sorting explicit enough when effectively it will sort by first entry to last? I would think it is generally better to sort by something that is relevant to the use case, but as with question 1 are there any conventions/style guides I should be aware of when naming fields?
Some info on my research:
I've looked for other questions on this site and Stack Overflow, Django's coding style guidelines, PEP8 but not found anything that directly answers this question. A question on Programmer's SE comes closest Should model/table names be as specific as possible. The points I take from the answers are:
naming should be as specific as possible
Table and field names should be descriptive of the domain entity that they capture