我是Django&Python的新手。 (以前是PHP)。我想在没有DB盲目的情况下归档自定义模型时遇到问题。该模型的数据来自原始SQL。
我试图找出如何从rawsql获取数据,然后将其插入Object的方法。我发现本文Model Manager看起来很有希望。
尝试&错误后。我收到以下错误:
环境:
请求方法:GET请求URL:http://127.0.0.1:8000/a/something
Django版本:2.1.5 Python版本:3.7.2已安装的应用程序: ['django.contrib.admin','django.contrib.auth', 'django.contrib.contenttypes','django.contrib.sessions', 'django.contrib.messages','django.contrib.staticfiles', 'rest_framework','rest_framework.authtoken','phonenumber_field', 'user_accounts','shipments','CA','rest_auth', 'rest_auth.registration','allauth','allauth.account', 'django.contrib.sites','allauth.socialaccount','dashboard'] 已安装的中间件: ['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']
跟踪:
文件 “ C:\ Users \ myfxhuta \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ django \ core \ handlers \ exception.py” 在内部 34. response = get_response(request)
文件 “ C:\ Users \ myfxhuta \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ django \ core \ handlers \ base.py” 在_get_response中 126. response = self.process_exception_by_middleware(e,request)
文件 “ C:\ Users \ myfxhuta \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ django \ core \ handlers \ base.py” 在_get_response中 124. response = wrapd_callback(request,* callback_args,** callback_kwargs)
文件 “ C:\ Users \ myfxhuta \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ django \ views \ decorators \ csrf.py” 在wrapped_view中 54. return view_func(* args,** kwargs)
文件 “ C:\ Users \ myfxhuta \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ django \ views \ generic \ base.py” 鉴于 68. return self.dispatch(request,* args,** kwargs)
文件 “ C:\ Users \ myfxhuta \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ rest_framework \ views.py” 在派遣中 495. response = self.handle_exception(exc)
文件 “ C:\ Users \ myfxhuta \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ rest_framework \ views.py” 在handle_exception中 455. self.raise_uncaught_exception(exc)
文件 “ C:\ Users \ myfxhuta \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ rest_framework \ views.py” 在派遣中 492. response = handler(request,* args,** kwargs)
文件 “ C:\ Users \ myfxhuta \ Documents \ Coding \ python-testing \ CA \ dashboard \ views.py” 在得到 23.“数据”:consignment_status.objects.with_counts()
文件 “ C:\ Users \ myfxhuta \ Documents \ Coding \ python-testing \ CA \ dashboard \ models.py” 在with_counts中 22. p = self.model(consignmentNo = row [0],status = row 1,created_time = row [2])
文件 “ C:\ Users \ myfxhuta \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ django \ db \ models \ base.py” 在初始化中 485.raise TypeError(“'%s'是该函数的无效关键字参数”%kwarg)
异常类型:/ a / something处的TypeError异常值:'status'为 此函数的关键字参数无效
这是我的 models.py
from django.db import models
class list_consignment(models.Manager):
def with_counts(self):
import pyodbc
cnxn = pyodbc.connect('Driver={SQL Server};Server=127.0.0.1;Database=TEST;uid=USERID;pwd=PASSWORD;')
cursor = cnxn.cursor()
with cnxn.cursor() as cursor:
cursor.execute("""select t.ConsignmentNo, case when t2.TaskId = '50' then t2.TaskName when t2.TaskId IS NULL then 'NEW' else 'PENDING' end as 'status', case when t2.TaskId IS NOT NULL then t2.ActEnd else t.CreateDate end as created_time from ( select ConsignmentNo,JobEntry.CreateDate,JobEntry.JobStatus, max(ActEnd) as lastes_time from JobEntry left join jobTracking on JobEntry.JobNo = jobTracking.JobNo and JobEntry.companyid=jobTracking.companyid where JobEntry.companyid = 40 and JobEntry.CustomerCode = 'A000177' and ConsignmentNo like '%' and CAST(JobEntry.CreateDate as date) >= CAST(getdate()-1 as date) and JobEntry.iscancel = 0 group by ConsignmentNo, JobEntry.CreateDate,JobEntry.JobStatus ) t left Join ( select ConsignmentNo,JobEntry.CreateDate as jcreate_date, jobTracking.* from JobEntry left join jobTracking on JobEntry.JobNo = jobTracking.JobNo and JobEntry.companyid=jobTracking.companyid where JobEntry.companyid = 40 and JobEntry.BranchId = 40 and CAST(JobEntry.CreateDate as date) >= CAST(getdate()-1 as date) and JobEntry.CustomerCode = 'A000177' and ConsignmentNo like '%' )t2 on t.ConsignmentNo = t2.ConsignmentNo and t.lastes_time = t2.ActEnd and t.CreateDate = t2.jcreate_date order by status, ConsignmentNo""")
result_list = []
for row in cursor.fetchall():
p = self.model(consignmentNo=row[0], status=row[1], created_time=row[2])
result_list.append(p)
return result_list
class consignment_status(models.Model):
consignmentNo = models.CharField(max_length=100)
status = models.TextField
created_time = models.DateField()
objects = list_consignment()
class Meta:
managed = False
这是我的 view.py
class dashboard(APIView):
permission_classes=(AllowAny,)
def get(APIView, request):
data = [{
"data" : consignment_status.objects.with_counts()
}]
return HttpResponse(data)
请提供指导,这是什么错误?
答案 0 :(得分:1)
在consignment_status类更改中,
status = models.TextField
到
status = models.TextField(max_length=100)