In django-tastypie, can choices be displayed in schema?

你。 提交于 2019-12-06 08:42:53

问题


I am trying to figure out whether I can represent model field choices to clients consuming a tastypie 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'

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
    }]
}

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"
    }]
}

But how would the client know the available choices for the status field without knowing the inner workings of SomeModel?

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.

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.

Thanks for any feedback!


回答1:


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

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.




回答2:


A simpler solution is to hack the choices information into your help_text blurb.

In our example we were able to do:

source = models.CharField(
    help_text="the source of the document, one of: %s" % ', '.join(['%s (%s)' % (t[0], t[1]) for t in DOCUMENT_SOURCES]),
    choices=DOCUMENT_SOURCES,
)

Easy peasy, automatically stays up to date, and is pretty much side-effect free.



来源:https://stackoverflow.com/questions/12611038/in-django-tastypie-can-choices-be-displayed-in-schema

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!