如何使用Django-rest-framework创建与模型不同的视图 [英] How do I create a view different than the model with Django-rest-framework
问题描述
所以,我有这两类服务器和数据中心;
So, I have this 2 classes of servers and datacenters;
class Datacenter(models.Model):
name = models.CharField(max_length=50)
status = models.CharField(max_length=50)
def __unicode__(self):
return self.name
class Servers(models.Model):
datacenter = models.ForeignKey(Datacenter)
hostname = models.CharField(max_length=50)
def __unicode__(self):
return self.hostname
创建一个视图,返回数据中心的详细信息以及所有相关的服务器,所以现在就这样做;
And want to create a view that returns the details of the datacenter plus all the servers that are related, so right now when I do;
http://127.0.0.1:8000/datacenter/1/
我得到的东西像
{
"id": 1,
"name": "TestDC"
}
但是我实际上想要得到的是这样的东西;
But what I'm actually looking to get is something like this;
{
"id": 1,
"name": "TestDC",
"Servers": [
{
"id": 1,
"hostname": "Server1",
},
{
"id": 2,
"hostname": "Server2",
}
]
}
现在我的看法是这样的;
Right now my view is this;
class DatacenterViewSet(viewsets.ModelViewSet):
queryset = datacenter.objects.all()
serializer_class = datacenterSerializer
和我的serialiazer;
and my serialiazer;
class DatacenterSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Datacenter
fields = ('id','name')
我也希望以其他方式使用该服务器列表:
I also would like to have that server list in an other method like;
http://127.0.0.1:8000/datacenter/1/Servers
任何建议?
推荐答案
嵌套服务器:
如果你想要(几乎)你给出的输出作为样本,那就是这样的:
If you want (almost) exactly the output you gave as a sample, then it would be this:
class ServersSerializer(serializers.ModelSerializer):
class Meta:
model = Servers
fields = ('id', 'hostname')
class DatacenterSerializer(serializers.ModelSerializer):
servers = ServersSerializer(source='servers_set')
class Meta:
model = Datacenter
fields = ('id', 'name')
如果要显示两个模型的所有字段,然后只需删除fields行。
If you want to show all fields for both models, then just drop the 'fields' line.
这也可以在没有source关键字参数的情况下工作,但需要相关名称才能匹配servers属性名称您可以通过将相关名称=服务器添加到Serv上的数据中心字段来实现ers模型)。
This could also work without the source keyword argument, but would require the related name to match the 'servers' property name (you could do this by adding related_name='servers' to the datacenter field on the Servers model).
DRF的文档相当不错,你关心的是序列化器关系
The docs for DRF are pretty good, the bits you care about are serializer relations
深度URL:
要实现嵌套的URL结构,您可以简单地创建一个与上述匹配的URL格式:
To achieve the nested URL structure, you could simply make an url pattern that matches the above like so:
url(r'^datacenter/(?P<datacenter_id>\d+)/Servers$', 'views.dc_servers',name="dc_servers")
这将以数据中心的ID作为kwarg datacenter_id
调用您的视图。然后,您可以使用该ID通过datacenter_id过滤视图的查询。
which would call your view with the ID of the Datacenter as the kwarg datacenter_id
. You would then use that ID to filter the queryset of your view by datacenter_id.
您必须查看如何自己编写该视图,这里是查看文档,让您开始。
You'll have to look into how to write that view yourself, here are the views docs to get you started.
几个通用Django提示:模型通常应该有单数名称而不是复数,并且添加一个related_name参数通常是一件好事(显式超过隐式)。
A couple of general Django tips: Models should usually have singular names rather than plural and adding a related_name argument is usually a good thing (explicit over implicit).
这篇关于如何使用Django-rest-framework创建与模型不同的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!