[Python] Requesting a specific view in Django multi-tenant API

Discussão em 'Python' iniciado por Stack, Abril 10, 2025.

  1. Stack

    Stack Membro Participativo

    Created a multi-tenant API in Django 2.2.17 using django-tenant-schemas==1.10.0 and currently testing it using Postman.

    I'm able to create a new organization (which is the Tenant) and GET them all

    [​IMG]

    Thing is, in the GET and POST requests to the endpoint http://127.0.0.1:8000/employee/ I'm getting


    AttributeError at /employee/

    'str' object has no attribute 'get'
    ​

    [​IMG]

    Here's my employee/views.py

    from django.shortcuts import render
    from employee.models import Employee
    from organization.models import Organization
    from django.http import JsonResponse,request, HttpResponse
    from django.views import View
    from tenant_schemas.utils import schema_context
    import json

    # Create your views here.
    class EmployeeView(View):

    def get(self, request, *args, **kwargs):
    try:
    organizations = Organization.objects.get(domain_url=request.META['HTTP_HOST'])
    schema_name = organizations["schema_name"]
    #schema_name = organizations.schema_name
    except Organization.DoesNotExist:
    return "no organization"

    with schema_context(schema_name):
    employees = Employee.objects.all()
    data = {"results": list(employees.values("id","name"))}
    return JsonResponse(data)


    def post(self, request, *args, **kwargs):
    try:
    organizations = Organization.objects.get(domain_url=request.META['HTTP_HOST'])
    schema_name = organizations.schema_name
    except Organization.DoesNotExist:
    return "no organization"

    with schema_context(schema_name):
    name = json.loads(request.body)['name']
    employee = Employee(name=name)
    employee.save()
    return "Employee added successfully"


    Here's the output in the terminal

    Internal Server Error: /employee/
    Traceback (most recent call last):
    File "C:\Users\tiago\Desktop\django\venv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
    File "C:\Users\tiago\Desktop\django\venv\lib\site-packages\django\utils\deprecation.py", line 96, in __call__
    response = self.process_response(request, response)
    File "C:\Users\tiago\Desktop\django\venv\lib\site-packages\django\middleware\clickjacking.py", line 26, in process_response
    if response.get('X-Frame-Options') is not None:
    AttributeError: 'str' object has no attribute 'get'
    [18/Jan/2021 19:56:01] "GET /employee/ HTTP/1.1" 500 64243


    and here is the Traceback in Postman

    Environment:


    Request Method: GET
    Request URL: http://127.0.0.1:8000/employee/

    Django Version: 2.2.17
    Python Version: 3.9.0
    Installed Applications:
    ['tenant_schemas',
    'organization',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'employee']
    Installed Middleware:
    ['tenant_schemas.middleware.TenantMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware']



    Traceback:

    File "C:\Users\tiago\Desktop\django\venv\lib\site-packages\django\core\handlers\exception.py" in inner
    34. response = get_response(request)

    File "C:\Users\tiago\Desktop\django\venv\lib\site-packages\django\utils\deprecation.py" in __call__
    96. response = self.process_response(request, response)

    File "C:\Users\tiago\Desktop\django\venv\lib\site-packages\django\middleware\clickjacking.py" in process_response
    26. if response.get('X-Frame-Options') is not None:

    Exception Type: AttributeError at /employee/
    Exception Value: 'str' object has no attribute 'get'

    Continue reading...

Compartilhe esta Página