16

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)

3 Answers 3

22

Try the following. Create serializer for your Level model:

class LevelSerializer(serializers.ModelSerializer):
    class Meta:
        model = Level

Then, inside LevelProcessSerializer, include LevelSerializer like this:

class LevelProcessSerializer(serializers.ModelSerializer):
    level = LevelSerializer(read_only=True)

    class Meta:
        model = LevelProcess

Usage in your ModelViewset:

class ViewLevelProcessViewSet(viewsets.ModelViewSet):
    queryset = LevelProcess.objects.all() 
    serializer_class = LevelProcessSerializer

This way, your json will look something like this:

{
   "id": 1,
   "level": {
      "id": 3,
      "status": "red"
   }
}

Hope this helps!

Sign up to request clarification or add additional context in comments.

8 Comments

@uetoaya this is assuming that level inside your LevelProcess is a foreign key. If not, let me know
yes.. level_id is a foreign key in LevelProcess
what about queryset in myviews?
i try your code and got this error could not automatically determine the name from the viewset, as it does not have a '.queryset' attribute.
@uetoaya see usage in the answer. The error you got is because of something else, I think. Please, paste your code and I will try to help
|
0
class LevelSerializer(serializers.ModelSerializer):
    class Meta:
        model = Level
        fields="__all__"

class LevelProcessSerializer(serializers.ModelSerializer):
    level = LevelSerializer(read_only=True)

class Meta:
    model = LevelProcess
    fields= "__all__"

Comments

-3

Don't try to "join" tables. This isn't SQL.

I am assuming your model look like Following,

class Level(models.Model):
    .......

class LevelProcess(models.Model):
    level = models.ForeignKey(Level)

Now, let's walk for query,

l = Level.objects.filter(id=level_id).first()
lp = l.level_process_set.all()

This is how we do in Django ORM.

1 Comment

Doesn't this result in multiple queries for each related LevelProcess? How is that preferable? It is ultimately SQL, you can't just pretend there's not a database engine behind this thing...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.