在序列化器Django Rest Framework中使用@StaticMethods [英] Use @StaticMethods in serializer Django Rest Framework
问题描述
我有两个使用相同功能的序列化器。我想将其定义为静态方法并重用。
I have two serializers which use a same function. I want to define it as a static method and reuse it.
文章序列化器
class ArticleDetailSerializer(ModelSerializer):
liked = SerializerMethodField()
class Meta:
model = Article
fields = [
'id',
'self_liked',
'title'
]
def get_liked(self, obj):
request = self.context.get('request')
self_like_obj = Reaction.objects.filter(user=request.user.id, content_type=ContentType.objects.get(model='article'), object_id=obj.id)
if self_like_obj.exists():
self_like = Reaction.objects.get(user=request.user.id, content_type=ContentType.objects.get(model='article'), object_id=obj.id).react_type
else:
self_like = False
return self_like
序列化器以发表评论
class CommentSerializer(ModelSerializer):
liked = SerializerMethodField()
class Meta:
model = Comment
fields = [
'id',
'self_liked',
'content'
]
def get_liked(self, obj):
request = self.context.get('request')
self_like_obj = Reaction.objects.filter(user=request.user.id, content_type=ContentType.objects.get(model='comment'), object_id=obj.id)
if self_like_obj.exists():
self_like = Reaction.objects.get(user=request.user.id, content_type=ContentType.objects.get(model='comment'), object_id=obj.id).react_type
else:
self_like = False
return self_like
如您所见,两个序列化程序使用一个通用函数: get_liked
如何将其定义为静态方法
As you see, two serializer use a general function: get_liked
How can I define it as a static methods for reuse?
推荐答案
由于 @neverwalkaloner 建议您只能在mixin类中实现一次方法,并且将此类用作两个序列化器的父类。请记住,您的 get_liked
方法在 ArticleDetailSerializer
和 CommentSerializer $中是不同的c $ c>序列化器。
As @neverwalkaloner suggested you can implement method only once in mixin class, and use this class as parent for both serializers. Keep in mind that your get_liked
method is not the same in ArticleDetailSerializer
and CommentSerializer
serializers.
ArticleDetailSerializer
方法具有 ContentType.objects.get(model ='article')
行,但 CommentSerializer
具有 ContentType.objects.get(model ='comment')
序列化器中 get_liked
方法之间的区别。
ArticleDetailSerializer
method has ContentType.objects.get(model='article')
line but CommentSerializer
has ContentType.objects.get(model='comment')
which makes the difference between get_liked
method from your serializers.
您可以使用与 @neverwalkaloner 建议使用,而不是 ContentType.objects.get(model ='article')
和 ContentType.objects.get(model ='comment' )
行,您可以尝试使用 ContentType.objects.get(model = self.Meta.model .__ name __。lower())
。
You can use the same mixin that @neverwalkaloner suggested but instead of ContentType.objects.get(model='article')
and ContentType.objects.get(model='comment')
lines you can try with ContentType.objects.get(model=self.Meta.model.__name__.lower())
.
我认为应该看起来像这样:
I think it should look like this:
class LikedMixin(object):
def get_lowercased_model_name(self, obj):
return self.Meta.model.__name__.lower()
def get_liked(self, obj):
request = self.context.get('request')
model_name = self.get_lowercased_model_name()
self_like_obj = Reaction.objects.filter(user=request.user.id, content_type=model_name, object_id=obj.id)
if self_like_obj.exists():
self_like = Reaction.objects.get(user=request.user.id, content_type=model_name, object_id=obj.id).react_type
else:
self_like = False
return self_like
这篇关于在序列化器Django Rest Framework中使用@StaticMethods的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!