Detect Django rest API conditions for accepting data before submitting

116 views Asked by At

I've an API end point that accepts multiple choices to select special situations in a person page, like: disabilities or chronic disease. Django rest framework requires a list and gives this error when I send other type like string.

"special_situation":["Expected a list of items but got type \"str\"."]

However when I check the options of the endpoint I don't get an indication of that requirement:

HTTP 200 OK
Allow: GET, POST, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "name": "Special Situation List",
    "description": "API endpoint that allows special situations to be viewed or edited.",
    "renders": [
        "application/json",
        "text/html"
    ],
    "parses": [
        "application/json",
        "application/x-www-form-urlencoded",
        "multipart/form-data"
    ],
    "actions": {
        "POST": {
            "url": {
                "type": "field",
                "required": false,
                "read_only": true,
                "label": "Url"
            },
            "name": {
                "type": "string",
                "required": true,
                "read_only": false,
                "label": "Name",
                "max_length": 50
            }
        }
    }
}

I'm using HyperlinkedModelSerializer and endpoint is called at:

http://127.0.0.1:8000/myapi/person/

and

http://127.0.0.1:8000/myapi/special_situation/

The end point is linked from person endpoint where I'm sending values based on the options of special_situation, the options for person end point also doesn't show the list requirement:

            "special_situation": {
                "type": "field",
                "required": false,
                "read_only": false,
                "label": "Special situation"
            }

In the person module I've:

special_situation = ParentalManyToManyField('needy.SpecialSituation', blank=True, verbose_name='special situation')

And for SpecialSituation:

@register_snippet
class SpecialSituation(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=50)

    panels = [
        FieldPanel('name'),
    ]

    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = 'special situations'

I thought the "type": "field" indicates a list it self but it's not acceptable in other endpoints without multiple choices. like in the charity end point.

"charity":["Incorrect type. Expected URL string, received list."]

full source code is availbe at: https://gitlab.com/uak/needy/-/tree/api_development

so the issue is that there is no indication that this end point requires a list except in the error message. I would like to be able to detect accepted type before I submit data to be able to dynamically construct a form in my GUI app.

I know I can manually detect the field name and create a list but would like a dynamic/automated without hard coding fields with different requirements.

0

There are 0 answers