I have built a project with a React frontend and a Django Rest Framework (DRF) API for my backend. Things are going well but I am quite curious about best practices for data saved against your models.
As an example, I have a user model with first_name
and last_name
fields. There is a one-to-one relationship between my user and the two user domains (let's say Buyer
and Seller
as an example). In addition, we have a foreign key relationship for Seller
on something like a Product
.
On my UI, I have tables that display the data and in general, the majority of tables display the user's full name (first_name
+ last_name
).
Generally, all table fields I will want to filter by and order by via API (eg. {base_url}/product/?ordering=full_name
).
I decided that I wanted the data returned from the REST API to represent the table data so I am now returning full_name
by augmenting the serializer of Buyer
and Seller
to have full name using a SerializerMethodField
like so:
full_name = serializers.SerializerMethodField()
...
def get_full_name(self, obj) -> str:
return obj.user.get_full_name()
However, I will also need to do this in all places where I want to show the Buyer
/Seller
where they are referenced by a Foreign Key.
So let's say we need it in the Product
, I would have something like:
seller_first_name = serializers.CharField(source='seller.user.first_name')
seller_last_name = serializers.CharField(source='seller.user.last_name')
seller_full_name = serializers.SerializerMethodField()
...
def get_seller_full_name(self, obj) -> str:
return obj.seller.user.get_full_name()
I guess I'm curious if it would be best to store all of these fields directly on the user object (first_name
, last_name
, and full_name
). There is clearly some duplication here, so I assume Django would have a way for me to auto-write full_name
based on first_name
and last_name
without requiring it to be passed in the API.
I'm sure it can be done either way but this is my first project using DRF as a backend and I would rather hear some other's experience now rather than learn myself in a year and have to do a large refactoring later on.
Thanks for any advice in advance.
All the best,
Brandon