Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django-REST: Nested relationships vs PrimaryKeyRelatedField

Is it better to use nested relationships or PrimaryKeyRelated field if you have lots of data?

I have a model with deep relationships.
For simplicity I did not add the colums.

Model:

Model

Usecase:

  1. User creates 1 Workoutplan with 2 Workouts and 3 WorkoutExercises.
  2. User creates 6 Sets for each WorkoutExercise/Exercise.
  3. User starts workout > new FinishedWorkout is created
  4. User does first exercise and enters the used weights > new FinishedWorkoutExercise with FinishedSet is created

Question:

I want to track the progression for each workoutplan > workout > exercise. So with time the user may have finished dozens of workouts therefore hundreds if sets are already in the database.

If I now use nested Relationships I may load a lot of data I don't need. But if I use PrimaryKeyRelatedFields I have to load all the data I need separately which means more effort in my frontend.

Which method is preferred in such a situation?

Edit:
If I use PrimaryKeyRelatedFields how do I distinguish if e.g. Workouts in Workoutplan is an array with primary keys or an array with the loaded objects?

like image 955
JDurstberger Avatar asked Nov 14 '15 14:11

JDurstberger


People also ask

What is the difference between ModelSerializer and HyperlinkedModelSerializer?

The HyperlinkedModelSerializer class is similar to the ModelSerializer class except that it uses hyperlinks to represent relationships, rather than primary keys. By default the serializer will include a url field instead of a primary key field.

Why serializers are used in Django?

Serializers in Django REST Framework are responsible for converting objects into data types understandable by javascript and front-end frameworks. Serializers also provide deserialization, allowing parsed data to be converted back into complex types, after first validating the incoming data.

What is PrimaryKeyRelatedField?

PrimaryKeyRelatedField may be used to represent the target of the relationship using its primary key. For example, the following serializer: class AlbumSerializer(serializers. ModelSerializer): tracks = serializers.

What is SlugRelatedField Django?

The SlugRelatedField provided by Django REST framework, like many of the related fields, is designed to be used with objects that already exist. Since you are looking to reference objects which already exist, or object which need to be created, you aren't going to be able to use it as-is.


1 Answers

If you use PrimaryKeyRelatedField, you'll have a big overload to request the the necessary data in frontend

In your case, I would create specific serializers with the fields you want (using Meta.fields attribute). So, you won't load unecessary data and the frontend won't need to request more data from backend.

I can write a sample code, if you need more details.

like image 65
Danilo Freitas Avatar answered Oct 04 '22 22:10

Danilo Freitas