Django exception shows url value in array

Viewed 75

I get an exception No connection adapters were found for "['https://uk-hairloom.atlassian.net/']/rest/agile/1.0/board?maxResults=50" when hitting my test_connection method.

When I trace this back to the view and print the POST request object in a standard django view, the values are wrapped in an array and I don't understand why. Weird thing is if I do a GET for all objects using postman, they are not wrapped in array and appear as strings. FYI model fields are TextField NOT list.

class JiraDataViewSet(BaseModelViewSet):
    queryset = JiraData.objects.all().order_by("-id")
    serializer_class = JiraDataSerializer
    permission_classes = [
        IsAdmin,
    ]
    lookup_field = "uuid"

@action(detail=False, methods=["post"])
def test_connection(self, request, *args, **kwargs):

    result = False
    if request.data:
        try:
            details = JiraData(**request.data)
            if details:
                if test_get_all_boards(details):
                    result = True
        except Exception as e:
            print('printing exception')
            print(e)
            pass

Exception looks like:

No connection adapters were found for "['https://uk- 
hairloom.atlassian.net/']/rest/agile/1.0/board?maxResults=50"

Any advice much appreciated.

1 Answers

request.POST and request.GET are instances of django.http.QueryDict. Quoting the documentation:

In an HttpRequest object, the GET and POST attributes are instances of django.http.QueryDict, a dictionary-like class customized to deal with multiple values for the same key. This is necessary because some HTML form elements, notably , pass multiple values for the same key.

Considering this it is not weird that they are represented as a list. Accessing the querydicts values using __getitem__ (request.POST['some_key']) or .get('some_key') according to the documentation works like:

Returns the value for the given key. If the key has more than one value, it returns the last value.

If we wanted the list of the values for a given key we could use it's getlist method:

values = request.POST.getlist('some_key') # returns a list of values or an empty list if matching key doesn't exist
Related