【问题标题】:Requesting a specific view in Django multitenant API在 Django 多租户 API 中请求特定视图
【发布时间】:2021-04-23 02:11:12
【问题描述】:

在 Django 2.2.17 中使用 django-tenant-schemas==1.10.0 创建了一个多租户 API,目前正在使用 Postman 对其进行测试。

我能够创建一个新组织(即租户)并将它们全部获取

问题是,在对端点 http://127.0.0.1:8000/employee/ 的 GET 和 POST 请求中,我得到了

/employee/处的AttributeError

'str' 对象没有属性'get'

这是我的员工/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"

这是终端的输出

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

这是 Postman 中的 Traceback

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'

【问题讨论】:

  • 你能复制并过去完整的堆栈跟踪吗?它引用了会引发错误的行。
  • @schillingt 完成!

标签: python django api postman multi-tenant


【解决方案1】:

看起来您正在从视图中返回原始字符串。您需要返回HttpResponse 的实例(或使用render 等)

例如,您有以下内容:

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"

它到达了return "no organization" 代码分支,然后打破了 Django 的中间件期望。

【讨论】:

  • 对......我以前做过(因为导入显示)但不喜欢结果(总是得到“没有组织”)然后无缘无故地把它复杂化了.. . 谢谢!
  • (已达到投票上限,明天只能给+1)
猜你喜欢
  • 2019-12-04
  • 2023-03-20
  • 2022-07-13
  • 2023-04-02
  • 1970-01-01
  • 2015-07-08
相关资源
最近更新 更多