我不太清楚如何继续这样做并寻找能够保持最佳表现的答案。
我有以下模型:
class Contact(models.Model):
STATUS_VERIFIED = 'verified'
STATUS_PENDING = 'pending'
STATUS_CHOICES = (
(STATUS_VERIFIED, STATUS_VERIFIED),
(STATUS_PENDING, STATUS_PENDING),
)
FAVOURITE_A = 'fava'
FAVOURITE_B = 'favb'
FAVOURITE_NONE = 'none'
FAVOURITE_BOTH = 'AndB'
FAVOURITE_CHOICES = (
(FAVOURITE_A, FAVOURITE_A),
(FAVOURITE_B, FAVOURITE_B),
(FAVOURITE_NONE, FAVOURITE_NONE),
(FAVOURITE_BOTH, FAVOURITE_BOTH),
)
source = models.ForeignKey(Customer, related_name="source_user")
destination = models.ForeignKey(Customer, related_name="destination_user")
status = models.CharField(max_length=25, choices=STATUS_CHOICES)
createdAt = models.DateTimeField(auto_now_add=True, auto_now=False)
updatedAt = models.DateTimeField(auto_now_add=True, auto_now=False)
favourite = models.CharField(max_length=25, choices=FAVOURITE_CHOICES)
以下序列化程序:
class ContactSerializer(serializers.ModelSerializer):
source_uuid = serializers.SerializerMethodField('get_field_source_uuid')
source_email = serializers.SerializerMethodField('get_field_source_email')
source_first_name = serializers.SerializerMethodField('get_field_source_first_name')
source_last_name = serializers.SerializerMethodField('get_field_source_last_name')
destination_uuid = serializers.SerializerMethodField('get_field_destination_uuid')
destination_email = serializers.SerializerMethodField('get_field_destination_email')
destination_first_name = serializers.SerializerMethodField('get_field_destination_first_name')
destination_last_name = serializers.SerializerMethodField('get_field_destination_last_name')
def get_field_source_uuid(self, obj):
return obj.source.uuid
def get_field_source_email(self, obj):
return obj.source.email
def get_field_source_first_name(self, obj):
return obj.source.first_name
def get_field_source_last_name(self, obj):
return obj.source.last_name
def get_field_destination_uuid(self, obj):
return obj.destination.uuid
def get_field_destination_email(self, obj):
return obj.destination.email
def get_field_destination_first_name(self, obj):
return obj.destination.first_name
def get_field_destination_last_name(self, obj):
return obj.destination.last_name
class Meta:
model = Contact
fields = (
'source_uuid',
'source_email',
'source_first_name',
'source_last_name',
'destination_uuid',
'destination_email',
'destination_first_name',
'destination_last_name',
)
这是相关的视图:
class Addressbook(APIView):
def get(self, request):
try:
addressbook = Contact.objects.filter(Q(source=request.user) or Q(destination=request.user), status=Contact.STATUS_VERIFIED)
serializer = ContactSerializer(addressbook, many=True)
return Response(serializer.data)
except Customer.DoesNotExist:
content = {'status': 1}
return Response(content, status=status.HTTP_404_NOT_FOUND)
这是什么:
我有一个允许用户添加联系人的API。有两个用户。让我们称他们为source
和destination
,如模型中所示。他们是否是"联系人"或不是由其状态定义:Pending
或Verified
。用户可以检查待处理的联系请求,以及接受此类请求。这些都没有提供,因为它们工作正常。
用户可以检索他们的地址簿。 地址簿应返回已验证的联系人列表。这表示用户不应出现在所述地址簿中。
有什么问题:
如何告诉序列化程序使用source
或destination
将联系人添加到返回的数据中,具体取决于登录的用户。
因此,当source = request.user
和destination
是经过验证的联系人时,它应该只有return destination
。当destination=request.user
它应该只有return source
在seralized数据中。这显然适用于many=True
,因此同时适用。
问题 这可以通过过滤器语句中的OUTER JOINS来实现吗?或者我是否需要在python中使用序列化程序,我可能希望避免使代码尽可能简单和可重用。
答案 0 :(得分:0)
在序列化程序方法中,您可以访问request user
def get_field_emails(self,obj):
request = self.context.get('request')
user = request.user
#Here you check you user type
if your_user_is_destination:
return destination.email
else:
return source.email