问题描述
我想弄清楚我是否可以将模型字段choices
表示给使用了一个tastypie API 的客户.
I am trying to figure out whether I can represent model field choices
to clients consuming a tastypie API.
我有一个 django (1.4.1) 应用程序,我正在为其实现 django-tastypie (0.9.11) API.我有一个类似于以下的模型和模型资源:
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
与 hydrate/dehydrate 方法来显示状态的字符串值如下,这对客户有更多价值:
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 的架构描述中列出选项,以便客户端可以在创建/编辑对象时内省可用选项.但我只是不确定这是否是在 Tastspie 中开箱即用的东西,或者我是否应该分叉 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
我还没有测试过上面的代码,但我希望你能明白.请注意,仅当您使用从提供的 queryset
中获取的tastypie 的 ModelResource 时,才会设置 object_class
.
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 中,选项可以显示在模式中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!