Django Rest框架序列化程序排除深度为2的外键(Django Rest framework serializer exclude foreign key with depth 2)
我做了一个返回一个对象作为json数据的api。 我正在使用django-rest-framework及其序列化程序。 使用资源(ModelResource),我排除了一些字段,如名为“所有者”的属性。 其中一个领域是一个外键。 我想在api中显示这个字段(所以我使用depth = 2),但是我想排除返回对象中排除的相同字段。 有没有一个很好的方法来做到这一点(我已经尝试了几件事情,没有想要的结果)。
这是我的(简化)代码:在models.py中:
class MyObject(models.Model): name = models.CharField(max_length=256, blank=True) parent = models.ForeignKey('self', blank=True, null=True, default=None) and_some_otherfields = models.otherFields(....) owner = models.ForeignKey(User, null=True, blank=True, related_name='myobject_owner')
在resource.py中:
class MyObjectResource(ModelResource): model = MyObject exclude = ('owner','and some other fields',)
并在用于返回对象的视图中返回:
data = Serializer(depth=2).serialize(my_object) return Response(status.HTTP_200_OK, data)
在响应中,它排除了排除字段(正如我想要的和预期的那样)。
但在字段父,父我的myobject与我想隐藏的所有字段。
我正在寻找一种方式来表明,对于这个父对象,序列化程序应该使用相同的资源,或将secundary字段添加到排除列表....
如果我使用depth = 1,它只会显示它是否有父项([]),否则为null,我确实需要知道至少父项的ID。
I have made an api that returns an object as json data. I Am using the django-rest-framework and its serializer. Using the resources (ModelResource) I excluded some fields, like a property called 'owner'. One of the fields is a foreignkey to itselve. I want to show this field in the api (so I use depth=2), but I want to exclude the same fields as I excluded in the object returning. Is there a nice way to do this (I have tried several things without the wanted result).
This is my (simplified) code: in models.py:
class MyObject(models.Model): name = models.CharField(max_length=256, blank=True) parent = models.ForeignKey('self', blank=True, null=True, default=None) and_some_otherfields = models.otherFields(....) owner = models.ForeignKey(User, null=True, blank=True, related_name='myobject_owner')
in resource.py:
class MyObjectResource(ModelResource): model = MyObject exclude = ('owner','and some other fields',)
and in the view used to return the object it returns this:
data = Serializer(depth=2).serialize(my_object) return Response(status.HTTP_200_OK, data)
In the response it leaves out the exclude fields (as I wanted and expected).
but in the field parent, the parent myobject with all fields I want to hide.
I Am looking for a way to indicate that for this parent object, the serializer should use the same Resource, or add the secundary fields to the exclude list....
If I use depth =1 it only shows whether it has a parent ([]), or null if not, and i do need to know at least the parent's ID.
原文:https://stackoverflow.com/questions/12261973
最满意答案
试试这个:
//var url = "http://domain.com/ztest1.php?ai=85&ver=7"; var url = window.location.href; var idx = url.indexOf("?"); var idx2 = url.indexOf("&"); var hash = idx != -1 ? url.substring(idx+4, idx2) : "";
如果你只想在这段代码中使用85,你可以使用
hash = idx != -1 ? hash.substring(3, idx) : "";
hash = idx != -1 ? hash.substring(3, idx) : "";
Try with this:
//var url = "http://domain.com/ztest1.php?ai=85&ver=7"; var url = window.location.href; var idx = url.indexOf("?"); var idx2 = url.indexOf("&"); var hash = idx != -1 ? url.substring(idx+4, idx2) : "";
And if you want only the 85 in this code you can use
hash = idx != -1 ? hash.substring(3, idx) : "";
相关问答
更多-
如果这是整个代码,那么在脚本之后你就会丢失关闭头标记,我运行它并且只要显示不同的错误消息就可以了。 编辑 - 还添加了编译代码,其下面有document.ready的简写。
the man