Django Rest框架中的复合键? [英] Composite keys in Django Rest Framework?

查看:140
本文介绍了Django Rest框架中的复合键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究一个Django应用程序,用于跟踪多个网络环境中的VLAN使用情况,其他应用程序将使用它来分配新的安装VLAN。



该模型是一个层次结构:位置 - > VLANPool-> VLAN



因此,每个位置都有唯一的VLAN 100,可以是不同池的成员,在位置。客户端应用程序可以说在customer_networks池中给我下一个VLAN,然后对生成的VLAN执行各种操作 - 保留,重命名/注释,发布。



每个模型都有一个自动创建的id主键,但是用户/ API客户端真的关心名称字段(池)或VLAN ID(vlans)。



我有一个基本的REST API与Django REST框架,通过为我的应用程序中的每个模型创建一个ModelViewSet和ModelSerializer,并使用路由器注册ViewSets。但是,默认情况下使用pk,这对最终用户是无意义的。对于可浏览的API,您可以点击结果,但我可以在不知道数据库ID的情况下构建URL。



我喜欢我必须写多少代码才能到达我所在的位置,但是我需要改变哪种更改才能获得这样的API模式:

  / api / location / {location_name} 
/ api / location / {location_name} / pools
/ api / location / {location_name} / pools / {pool_name}
/ api / location / {location_name} / pools / {pool_name} / vlans
/ api / location / {location_name} / vlans / {vlan_id}

也就是说,查找键是location.name和实际对象的名称字段的组合。



<有人可以指向正确的方向吗?我想我需要更改Serializer上的查询器或创建一个新的Serializer,但是URL的构造如何?



我首先添加了lookup_field ='名称'到我的ViewSets,但是路由器说它无法使用名称而不是ID来解析URL(使用ID代替获取404,所以它实际上将该名称识别为有效)。

解决方案

您可以使用lookup_fields生成自定义网址。您可以在此处查看以下链接:多重查询精灵自定义网址


I've been working on a Django app for tracking VLAN usage in multiple network environments, which would be used by other apps to allocate VLANs for new installations.

The model is a hierarchy: Location->VLANPool->VLAN

So there is a unique VLAN 100 at each location, which could be a member of a different pool, depending on the location. Client applications can then say "give me the next VLAN in the customer_networks" pool, and then perform various operations on the resulting VLAN - reserve, rename/comment, release.

Each model has a auto-created 'id' primary key, but the user/API client really cares about either the name field (pools) or the VLAN ID (vlans).

I've got a basic REST API going with Django REST Framework, by creating a ModelViewSet and ModelSerializer for each model in my app and registering the ViewSets with a router. However, that uses the pk by default, which is meaningless to the end user. That's fine for the browsable API, where you can just click on results, but I'd like to be able to construct URLs without knowing about the database ID.

I like how little code I've had to write to get to where I am, but what is the minimum I need to change to get to an API schema like this:

/api/location/{location_name}
/api/location/{location_name}/pools
/api/location/{location_name}/pools/{pool_name}
/api/location/{location_name}/pools/{pool_name}/vlans
/api/location/{location_name}/vlans/{vlan_id}

That is, where the lookup key is a composite of the location.name and the actual object's name fields.

Can someone point me in the right direction? I guess I'll need to either change the queryset on the Serializer or create a new Serializer, but how about the URL construction?

I started out by adding lookup_field='name' to my ViewSets but then the router says it can't resolve URLs using the name instead of the id (using an ID instead gets a 404, so it is actually recognising the name as valid).

解决方案

You can generate the custom urls using the lookup_fields. Can you follow this link below here Multiple LookUp Args and Custom Urls.

这篇关于Django Rest框架中的复合键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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