如何在一个外键的queryset的细节领域包括(Django和rest_api) [英] How to include in queryset details fields of a foreign key (django and rest_api)

查看:193
本文介绍了如何在一个外键的queryset的细节领域包括(Django和rest_api)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为了显示聊天记录的查询集使用rest_api在Django。
我试图把它一会儿做,没有成功...

I use rest_api in django in order to display a queryset of "chats". I tried to get it done for a while, without success...

在angularjs控制器I调用哪个执行以下功能:

in angularjs controller I call a function which do the following:

$scope.conversations =   $http.get('/api/chats/').then(function(response){
    return response.data;
});

在rest_api应用程序,我把这个的urls.py:

in urls.py of the rest_api app I put this:

url(r'^chats/$', login_required(views.chatsViewSet.as_view()) ),

在我把这个rest_api的view.py:

in view.py of the rest_api I put this:

from rest_framework.generics import ListCreateAPIView
from serializers import ChatsSerializer

class ChatsViewSet(ListCreateAPIView):
    serializer_class = ChatsSerializer
    def get_queryset(self):
        return Message.objects._folder(('sender', 'recipient'), {},order_by='-sent_at')

和在rest_api serializers.py我把这个:

and in serializers.py in the rest_api I put this:

from postman.models import Message
from rest_framework import serializers

class ChatsSerializer(serializers.ModelSerializer):
  class Meta:
    model = Message
    fields = ('id', 'sender', 'recipient','thread','subject','moderation_reason','body')
    ordering =['-thread']

在发件人和邮递员模型的消息'收件人'字段的外键。

The 'sender' and the 'recipient' fields in the Message of postman model are foreign keys.

下面是发送者的定义,例如,在消息模型在邮递员:

Here is the definition of sender, for instance, in the Message Model in postman:

sender = models.ForeignKey(get_user_model(), related_name='sent_messages', null=True, blank=True, verbose_name=_("sender"))

我想在rest_api的查询集包括发件人的不仅是ID,而且它的用户名字段...

I would like that the queryset in rest_api include not only the ID of the sender but also its username field...

我想读一些职位,像rest_api使用额外的,但我没有弄明白。
我会很感激,如果有人可以给我写明确的指示怎么办呢......

I tried to read some posts like using 'extra' in rest_api but I didn't figure it out. I will be grateful if somebody could write me explicit instruction how to do it...

推荐答案

您可以对您的模型属性:

You can make a property on your model:

class Message(models.Model):
    # some code...

    @property
    def sender_name(self):
        return self.sender.name # username or whatever

然后就是你序列化器创建一个自定义字段:

Then is you serializer you create a custom field:

class ChatsSerializer(serializers.ModelSerializer):
    # some code...

    sender_name = serializers.Field(source = 'sender_name') # source = 'name of property or method' you dont have to pass it in this example becouse model property has the same name as this serializer attribute

    class Meta:
         fields = ('sender_name', 'id', 'sender', 'recipient','thread','subject','moderation_reason','body')

现在你有你的JSON响应的'SENDER_NAME'对象。

Now you have an 'sender_name' object in your JSON response.

这只是其中一个方法,我希望它能帮助:)

That's just one method I hope it helps :)

第二个是增加一个UserModelSerializer:

The second one is to add an UserModelSerializer:

class UserModelSerializer(serializers.ModelSerializer):

    class Meta:
         model = get_user_model()

class ChatsSerializer(serializers.ModelSerializer):
    # some code...

    sender = UserModelSerializer()
    recipient = UserModelSerializer()

    class Meta:
         fields = ('sender_name', 'id', 'sender', 'recipient','thread','subject','moderation_reason','body')

Retriving是确定的。但是,创建和使用它的更新是一个硬片编码。

Retriving is ok. But creating and updating with it is a hard piece of coding.

您可以随时为您的'方'的对象,并从这些'FK'UserModelView只有外键,请求数据reciving您的聊天消息后角服务,并绑定在一起。这是第三种方法。

You can always create an Angular service for your 'sender' object and after reciving your chat messages with only foreign keys, request data from a 'UserModelView' with those 'FK' and bind them together. That's a third method.

这篇关于如何在一个外键的queryset的细节领域包括(Django和rest_api)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆