django rest framework动态相关对象
django rest framework dynamic related object
问题 - 我有一个使用 django-rest-framework (django v1.7.7, django-rest-framework v3.1.1) 的 REST 服务器。在通知中,我会让用户知道他们是否收到了新的好友请求,或者是否获得了新的徽章。还有其他通知类型,但这个简单的例子可以解释我的问题。
在我的 GET 响应中,我想获得带有动态相关对象的通知,该对象由类型决定。如果类型是 friendreq
那么我希望 relatedObject 是一个带有 UserSerializer 的 User 实例。如果类型是 badge
,我想让 relatedObject 成为一个带有 BadgeSerializer 的 Badge 实例。
注意:我已经有了这些其他的序列化器(UserSerializer、BadgeSerializer)。
以下是我希望在回复中实现的目标:
{
"id": 1,
"title": "Some Title",
"type": "friendreq"
"relatedObject": {
// this is the User instance. For badge it would be a Badge instance
"id": 1,
"username": "foo",
"email": "foo@bar.com",
}
}
下面是我的模型和序列化程序:
# models.py
class Notification(models.Model):
"""
Notifications are sent to users to let them know about something. The
notifications will be about earning a badge, receiving friend request,
or a special message from the site admins.
"""
TYPE_CHOICES = (
('badge', 'badge'),
('friendreq', 'friend request'),
('system', 'system'),
)
title = models.CharField(max_length=30)
type = models.CharField(max_length=10, choices=TYPE_CHOICES)
user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name="user")
related_id = models.PositiveIntegerField(null=True, blank=True)
# serializers.py
class NotificationSerializer(serializers.ModelSerializer):
if self.type == "badge":
related_object = BadgeSerializer(
read_only=True,
queryset=Badge.objects.get(id=self.related_id)
)
elif self.type == "friendreq":
related_object = FriendRequestSerializer(
read_only=True,
queryset=FriendRequest.objects.get(id=self.related_id)
)
class Meta:
model = Notification
这段代码不起作用,但希望它能解释我想要完成的事情以及我想要去的方向。也许那个方向是完全错误的,我应该尝试使用其他方法来完成它。
我尝试的另一个选择是使用 SerializerMethodField
并在一个方法中执行此操作,但对于尝试 return 基于另一个字段的序列化对象的这种情况,这似乎不太干净.
我相信您想要使用的是 DRF 文档中提到的 .to_representation()
方法:http://www.django-rest-framework.org/api-guide/relations/#generic-relationships
问题 - 我有一个使用 django-rest-framework (django v1.7.7, django-rest-framework v3.1.1) 的 REST 服务器。在通知中,我会让用户知道他们是否收到了新的好友请求,或者是否获得了新的徽章。还有其他通知类型,但这个简单的例子可以解释我的问题。
在我的 GET 响应中,我想获得带有动态相关对象的通知,该对象由类型决定。如果类型是 friendreq
那么我希望 relatedObject 是一个带有 UserSerializer 的 User 实例。如果类型是 badge
,我想让 relatedObject 成为一个带有 BadgeSerializer 的 Badge 实例。
注意:我已经有了这些其他的序列化器(UserSerializer、BadgeSerializer)。
以下是我希望在回复中实现的目标:
{
"id": 1,
"title": "Some Title",
"type": "friendreq"
"relatedObject": {
// this is the User instance. For badge it would be a Badge instance
"id": 1,
"username": "foo",
"email": "foo@bar.com",
}
}
下面是我的模型和序列化程序:
# models.py
class Notification(models.Model):
"""
Notifications are sent to users to let them know about something. The
notifications will be about earning a badge, receiving friend request,
or a special message from the site admins.
"""
TYPE_CHOICES = (
('badge', 'badge'),
('friendreq', 'friend request'),
('system', 'system'),
)
title = models.CharField(max_length=30)
type = models.CharField(max_length=10, choices=TYPE_CHOICES)
user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name="user")
related_id = models.PositiveIntegerField(null=True, blank=True)
# serializers.py
class NotificationSerializer(serializers.ModelSerializer):
if self.type == "badge":
related_object = BadgeSerializer(
read_only=True,
queryset=Badge.objects.get(id=self.related_id)
)
elif self.type == "friendreq":
related_object = FriendRequestSerializer(
read_only=True,
queryset=FriendRequest.objects.get(id=self.related_id)
)
class Meta:
model = Notification
这段代码不起作用,但希望它能解释我想要完成的事情以及我想要去的方向。也许那个方向是完全错误的,我应该尝试使用其他方法来完成它。
我尝试的另一个选择是使用 SerializerMethodField
并在一个方法中执行此操作,但对于尝试 return 基于另一个字段的序列化对象的这种情况,这似乎不太干净.
我相信您想要使用的是 DRF 文档中提到的 .to_representation()
方法:http://www.django-rest-framework.org/api-guide/relations/#generic-relationships