在Django-tastypie,可以选择在架构中显示? [英] In django-tastypie, can choices be displayed in schema?
问题描述
我想弄清楚我是否可以present示范田选择
重新给客户消耗tastypie API。
I am trying to figure out whether I can represent model field choices
to clients consuming a tastypie API.
我有哪些我采取一个Django-tastypie(0.9.11)API一个Django(1.4.1)的应用。我有类似如下的型号和ModelResource:
I have a django (1.4.1) application for which I am implementing a django-tastypie (0.9.11) API. I have a Model and ModelResource similar to the following:
class SomeModel(models.Model):
QUEUED, IN_PROCESS, COMPLETE = range(3)
STATUS_CHOICES = (
(QUEUED, 'Queued'),
(IN_PROCESS, 'In Process'),
(COMPLETE, 'Complete'),
)
name = models.CharFIeld(max_length=50)
status = models.IntegerField(choices=STATUS_CHOICES, default=QUEUED)
class SomeModelResource(ModelResource):
class Meta:
queryset = SomeModel.objects.all()
resource_name = 'some_model'
当我看着API中的对象的名称和状态字段显示如下:
When I look at objects in the API, the name and status fields are displayed as follows:
{
...
"objects":[
{
"name": "Some name 1",
"status": 0
},
{
"name": "Some name 2",
"status": 2
}]
}
我知道我可以改变 SomeModelResource
与水合/脱水方法来进行状态显示字符串值如下,这将有更多的价值给客户:
I know I can alter SomeModelResource
with hydrate/dehydrate methods to display the string values for status as follows, which would have more value to clients:
{
...
"objects":[
{
"name": "Some name 1",
"status": "Queued"
},
{
"name": "Some name 2",
"status": "Complete"
}]
}
但如何将客户端知道状态字段可用的选择不知道SomeModel的内部运作?
But how would the client know the available choices for the status field without knowing the inner workings of SomeModel?
客户端创建的系统中的对象可能无法提供如 QUEUED的默认值
最好状态。但是,客户端是的编辑的对象需要知道可用选项的状态,以提供一个有效的选项。
Clients creating objects in the system may not provide a status as the default value of QUEUED
is desirable. But clients that are editing objects need to know the available options for status to provide a valid option.
我想为在SomeModelResource架构描述上市的选择,所以创建/编辑对象时,客户端可以内省可用的选项。但我只是不知道这是否是可用的东西开箱的tastypie,或者我是否应该叉tastypie引进的能力。
I would like for the choices to be listed in the schema description for SomeModelResource, so the client can introspect the available choices when creating/editing objects. But I am just not sure whether this is something available out of the box in tastypie, or if I should fork tastypie to introduce the capability.
感谢您的任何反馈!
推荐答案
您可以通过在资源覆盖方法添加的选择模式。如果你想的选择添加到任何领域(也许有许多资源使用),您可以如下创建方式:
You can add the choices to the schema by overriding the method in your resource. If you would want to add the choices to any field (maybe to use with many resources), you could create the method as follows:
def build_schema(self):
base_schema = super(SomeModelResource, self).build_schema()
for f in self._meta.object_class._meta.fields:
if f.name in base_schema['fields'] and f.choices:
base_schema['fields'][f.name].update({
'choices': f.choices,
})
return base_schema
我没有测试过上述code,但我希望你的想法。请注意, object_class
将只如果使用tastypie的ModelResource,因为它正在从被置所提供的查询集
。
I haven't tested the above code but I hope you get the idea. Note that the object_class
will be set only if you use the tastypie's ModelResource as it is being get from the provided queryset
.
这篇关于在Django-tastypie,可以选择在架构中显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!