Reputation: 15451
Created a multitenant 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
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'
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'
Upvotes: 1
Views: 192
Reputation: 13731
It looks like you're returning raw strings from your views. You need to return instances of HttpResponse
(or use render
, etc)
For example you have the following:
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"
It's hitting the return "no organization"
code branch which then breaks Django's middleware expectations.
Upvotes: 1