Let say i have two models:
level:
id
file_number
status
level_process:
process_ptr_id
level_id
I want to combine both of my table above to display it in one API using django-rest-framework.. I'm looking for the example on the internet and i cannot find it...by the way i'm using python 2.7 , django 1.10.5 and djangorestframework 3.6.2
serializer.py
class LevelSerializer(serializers.HyperlinkedModelSerializer):
id = serializers.ReadOnlyField()
class Meta:
model = Level
fields = ('__all__')
class LevelProcessSerializer(serializers.ModelSerializer):
level = LevelSerializer(read_only=True)
class Meta:
model = LevelProcess
fields = ('__all__')
views.py
class ViewLevelProcessViewSet(viewsets.ModelViewSet):
processes = LevelProcess.objects.all()
serializer_class = LevelProcessSerializer(processes, many=True)
Don't try to "join" tables. This isn't SQL.
I am assuming your model look like Following,
Now, let's walk for query,
This is how we do in Django ORM.
Try the following. Create serializer for your
Level
model:Then, inside
LevelProcessSerializer
, includeLevelSerializer
like this:Usage in your ModelViewset:
This way, your json will look something like this:
Hope this helps!