question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[QUESTION] How to use serializers from other domains for nested relationships

See original GitHub issue

While trying to implement the structure that you have posed, I have reached a point where I wonder what would be the best course of action:

In my use case, I have two domains, one that includes a User model, and another one that includes an Outfit model. I’m also using django serializers atm as they provide some helpers to ease development.

For my use case, I want to return the User model nested inside the Outfit model, but that would cross models between domains. What would be the best approach here?

from django.contrib.auth.models import User
from .models.look import Outfit

class UserSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = User
        fields = ['username', 'email', 'url']

class OutfitSerializer(serializers.HyperlinkedModelSerializer):
    user = UserSerializer()

    class Meta:
        model = Look
        fields = ['shortcode', 'user', 'url']

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
phaltcommented, Aug 20, 2019

This is a good question, and one my team has struggled with solving too. I briefly looked at some solutions around the internet. Things like protocol buffers try and solve this by setting up a shared resource of schemas for data. My only concern with that is: you then have the problem of controlling the distribution of that protocol buffer.

We solved this (we === the team at my current employment) by having our own UserSerializer in our domain. Because it was “our” version of the User, even though the User came from a different domain.

1reaction
phaltcommented, Aug 20, 2019

@pedrogimenez very good idea. I like it! Can you share any code examples? I can promote this issue into a question for prosperity.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Django Rest Framework relationship queries in nested serializer
1 Answer 1 ; PlayerSerializer(serializers.Serializer): player_id = serializers.IntegerField() first_name = serializers.CharField(max_length=256) ...
Read more >
Serializer relations - Django REST framework
Such nested relationships can be expressed by using serializers as fields. If the field is used to represent a to-many relationship, you should...
Read more >
Unique constraint prevents nested serializers from updating
The workaround this is to remove the unique constraint from the serializer and move it to the business logic validation part since the...
Read more >
Serializer Relations - Django REST Framework - GeeksforGeeks
In this section, we will discuss the different serializer relations provided by Django REST Framework Serializers. Table of Contents.
Read more >
Frequently Asked Questions - RMI and Object Serialization
2 Why does my remote method or "callback" routine fail with a nested java.net.UnknownHostException ? C.3 My server is using a fully qualified...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found