Polymorphically serializing objects in django
up vote
0
down vote
favorite
I have the following object inheritance model.
class Room:
name = models.CharField(db_index=True, unique=True, max_length=255)
status = models.CharField(default=RoomStatus.ACTIVE, max_length=256, null=True)
members = models.ManyToManyField(User)
last_activity = models.DateTimeField(default=timezone.now)
And the inherited models are,
class LeagueRoom(Room):
league = models.ForeignKey(League, on_delete=models.CASCADE, null=True)
location = models.ForeignKey(Location, on_delete=models.CASCADE, null=True)
logo_url = models.CharField(max_length=1024, null=True)
and
class ClubRoom(Room):
club = models.ForeignKey(Club, on_delete=models.CASCADE, null=True)
location = models.ForeignKey(Location, on_delete=models.CASCADE, null=True)
logo_url = models.CharField(max_length=1024, null=True)
The respective Serializers are as follows,
class RoomSerializer(serializers.ModelSerializer):
members = UserSerializer(read_only=True, many=True)
class Meta:
model = Room
fields = ('id', 'name', 'status', 'members', 'created', 'modified', 'last_active')
and
class LeagueRoomSerializer(serializers.ModelSerializer):
location = LocationSerializer(read_only=True)
league = LeagueSerializer(read_only=True)
room = RoomSerializer(read_only=True)
class Meta:
model = LeagueRoom
fields = ('id', 'name', 'location', 'status', 'league',
'logo_url', 'room', 'created', 'modified',)
and
class ClubRoomSerializer(serializers.ModelSerializer):
location = LocationSerializer(read_only=True)
club = ClubSerializer(read_only=True)
room = RoomSerializer(read_only=True)
class Meta:
model = ClubRoom
fields = ('id', 'name', 'location', 'club', 'logo_url',
'status', 'room', 'created', 'modified',)
My problem is that I have fetched all rooms for an user in the following manner.
rooms = user.room_set.order_by('-last_activity')
Now I want to Serialize this data based on the room type. Thus instead of using the RoomSerializer
I want to traverse the list of rooms and if the room is ClubRoom, then use ClubRoomSerializer
or else LeagueRoomSerializer
.
I'm not sure how to determine the child object from the parent.
Can someone help me with this.
python django django-orm
add a comment |
up vote
0
down vote
favorite
I have the following object inheritance model.
class Room:
name = models.CharField(db_index=True, unique=True, max_length=255)
status = models.CharField(default=RoomStatus.ACTIVE, max_length=256, null=True)
members = models.ManyToManyField(User)
last_activity = models.DateTimeField(default=timezone.now)
And the inherited models are,
class LeagueRoom(Room):
league = models.ForeignKey(League, on_delete=models.CASCADE, null=True)
location = models.ForeignKey(Location, on_delete=models.CASCADE, null=True)
logo_url = models.CharField(max_length=1024, null=True)
and
class ClubRoom(Room):
club = models.ForeignKey(Club, on_delete=models.CASCADE, null=True)
location = models.ForeignKey(Location, on_delete=models.CASCADE, null=True)
logo_url = models.CharField(max_length=1024, null=True)
The respective Serializers are as follows,
class RoomSerializer(serializers.ModelSerializer):
members = UserSerializer(read_only=True, many=True)
class Meta:
model = Room
fields = ('id', 'name', 'status', 'members', 'created', 'modified', 'last_active')
and
class LeagueRoomSerializer(serializers.ModelSerializer):
location = LocationSerializer(read_only=True)
league = LeagueSerializer(read_only=True)
room = RoomSerializer(read_only=True)
class Meta:
model = LeagueRoom
fields = ('id', 'name', 'location', 'status', 'league',
'logo_url', 'room', 'created', 'modified',)
and
class ClubRoomSerializer(serializers.ModelSerializer):
location = LocationSerializer(read_only=True)
club = ClubSerializer(read_only=True)
room = RoomSerializer(read_only=True)
class Meta:
model = ClubRoom
fields = ('id', 'name', 'location', 'club', 'logo_url',
'status', 'room', 'created', 'modified',)
My problem is that I have fetched all rooms for an user in the following manner.
rooms = user.room_set.order_by('-last_activity')
Now I want to Serialize this data based on the room type. Thus instead of using the RoomSerializer
I want to traverse the list of rooms and if the room is ClubRoom, then use ClubRoomSerializer
or else LeagueRoomSerializer
.
I'm not sure how to determine the child object from the parent.
Can someone help me with this.
python django django-orm
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have the following object inheritance model.
class Room:
name = models.CharField(db_index=True, unique=True, max_length=255)
status = models.CharField(default=RoomStatus.ACTIVE, max_length=256, null=True)
members = models.ManyToManyField(User)
last_activity = models.DateTimeField(default=timezone.now)
And the inherited models are,
class LeagueRoom(Room):
league = models.ForeignKey(League, on_delete=models.CASCADE, null=True)
location = models.ForeignKey(Location, on_delete=models.CASCADE, null=True)
logo_url = models.CharField(max_length=1024, null=True)
and
class ClubRoom(Room):
club = models.ForeignKey(Club, on_delete=models.CASCADE, null=True)
location = models.ForeignKey(Location, on_delete=models.CASCADE, null=True)
logo_url = models.CharField(max_length=1024, null=True)
The respective Serializers are as follows,
class RoomSerializer(serializers.ModelSerializer):
members = UserSerializer(read_only=True, many=True)
class Meta:
model = Room
fields = ('id', 'name', 'status', 'members', 'created', 'modified', 'last_active')
and
class LeagueRoomSerializer(serializers.ModelSerializer):
location = LocationSerializer(read_only=True)
league = LeagueSerializer(read_only=True)
room = RoomSerializer(read_only=True)
class Meta:
model = LeagueRoom
fields = ('id', 'name', 'location', 'status', 'league',
'logo_url', 'room', 'created', 'modified',)
and
class ClubRoomSerializer(serializers.ModelSerializer):
location = LocationSerializer(read_only=True)
club = ClubSerializer(read_only=True)
room = RoomSerializer(read_only=True)
class Meta:
model = ClubRoom
fields = ('id', 'name', 'location', 'club', 'logo_url',
'status', 'room', 'created', 'modified',)
My problem is that I have fetched all rooms for an user in the following manner.
rooms = user.room_set.order_by('-last_activity')
Now I want to Serialize this data based on the room type. Thus instead of using the RoomSerializer
I want to traverse the list of rooms and if the room is ClubRoom, then use ClubRoomSerializer
or else LeagueRoomSerializer
.
I'm not sure how to determine the child object from the parent.
Can someone help me with this.
python django django-orm
I have the following object inheritance model.
class Room:
name = models.CharField(db_index=True, unique=True, max_length=255)
status = models.CharField(default=RoomStatus.ACTIVE, max_length=256, null=True)
members = models.ManyToManyField(User)
last_activity = models.DateTimeField(default=timezone.now)
And the inherited models are,
class LeagueRoom(Room):
league = models.ForeignKey(League, on_delete=models.CASCADE, null=True)
location = models.ForeignKey(Location, on_delete=models.CASCADE, null=True)
logo_url = models.CharField(max_length=1024, null=True)
and
class ClubRoom(Room):
club = models.ForeignKey(Club, on_delete=models.CASCADE, null=True)
location = models.ForeignKey(Location, on_delete=models.CASCADE, null=True)
logo_url = models.CharField(max_length=1024, null=True)
The respective Serializers are as follows,
class RoomSerializer(serializers.ModelSerializer):
members = UserSerializer(read_only=True, many=True)
class Meta:
model = Room
fields = ('id', 'name', 'status', 'members', 'created', 'modified', 'last_active')
and
class LeagueRoomSerializer(serializers.ModelSerializer):
location = LocationSerializer(read_only=True)
league = LeagueSerializer(read_only=True)
room = RoomSerializer(read_only=True)
class Meta:
model = LeagueRoom
fields = ('id', 'name', 'location', 'status', 'league',
'logo_url', 'room', 'created', 'modified',)
and
class ClubRoomSerializer(serializers.ModelSerializer):
location = LocationSerializer(read_only=True)
club = ClubSerializer(read_only=True)
room = RoomSerializer(read_only=True)
class Meta:
model = ClubRoom
fields = ('id', 'name', 'location', 'club', 'logo_url',
'status', 'room', 'created', 'modified',)
My problem is that I have fetched all rooms for an user in the following manner.
rooms = user.room_set.order_by('-last_activity')
Now I want to Serialize this data based on the room type. Thus instead of using the RoomSerializer
I want to traverse the list of rooms and if the room is ClubRoom, then use ClubRoomSerializer
or else LeagueRoomSerializer
.
I'm not sure how to determine the child object from the parent.
Can someone help me with this.
python django django-orm
python django django-orm
asked Nov 8 at 23:51
Melissa Stewart
754622
754622
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
class RoomSerializer(serializers.Serializer):
def to_representation(self, instance):
if isinstance(instance, LeagueRoom)
serializer_class = LeagueRoomSerializer
elif isinstance(instance, ClubRoom):
serializer_class = ClubRoomSerializer
return serializer_class(instance=instance).data
queryset = user.room_set.order_by('-last_activity')
serialized = RoomSerializer(queryset, many=True).data
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
class RoomSerializer(serializers.Serializer):
def to_representation(self, instance):
if isinstance(instance, LeagueRoom)
serializer_class = LeagueRoomSerializer
elif isinstance(instance, ClubRoom):
serializer_class = ClubRoomSerializer
return serializer_class(instance=instance).data
queryset = user.room_set.order_by('-last_activity')
serialized = RoomSerializer(queryset, many=True).data
add a comment |
up vote
1
down vote
accepted
class RoomSerializer(serializers.Serializer):
def to_representation(self, instance):
if isinstance(instance, LeagueRoom)
serializer_class = LeagueRoomSerializer
elif isinstance(instance, ClubRoom):
serializer_class = ClubRoomSerializer
return serializer_class(instance=instance).data
queryset = user.room_set.order_by('-last_activity')
serialized = RoomSerializer(queryset, many=True).data
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
class RoomSerializer(serializers.Serializer):
def to_representation(self, instance):
if isinstance(instance, LeagueRoom)
serializer_class = LeagueRoomSerializer
elif isinstance(instance, ClubRoom):
serializer_class = ClubRoomSerializer
return serializer_class(instance=instance).data
queryset = user.room_set.order_by('-last_activity')
serialized = RoomSerializer(queryset, many=True).data
class RoomSerializer(serializers.Serializer):
def to_representation(self, instance):
if isinstance(instance, LeagueRoom)
serializer_class = LeagueRoomSerializer
elif isinstance(instance, ClubRoom):
serializer_class = ClubRoomSerializer
return serializer_class(instance=instance).data
queryset = user.room_set.order_by('-last_activity')
serialized = RoomSerializer(queryset, many=True).data
answered Nov 9 at 0:09
rikAtee
4,61042855
4,61042855
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53217869%2fpolymorphically-serializing-objects-in-django%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown