当我尝试使用更新或创建查询时,它会给出错误FieldError: Invalid field name(s) for model
这是我的models.py
class Base(models.Model):
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
created_by = models.ForeignKey(User)
class Meta:
abstract = True
class Graph(Base):
sensor = models.ForeignKey("Sensor")
name = models.CharField(max_length=50)
query = models.TextField()
javascript = models.TextField()
args = models.TextField()
server_details = models.ForeignKey("ServerDetails")
现在我想更新或创建Graph的查询
这是我试图做的事情
@is_login_valid
def save_graph(request):
params = json.loads(request.body)
ser_obj = ServerDetails.objects.get(id=params['server_details']['id'])
sensor = Sensor.objects.get(id=1)
rrs_obj, created = Graph.objects.update_or_create(
id=params.get('id'),
defaults={
"name": params.get('name'),
"query": params.get('query'),
"javascript": params.get('javascript'),
"args": params.get('args'),
"server_details": ser_obj,
"created_by": request.user,
"sensor": sensor
}
)
return JsonResponse({
"validation": "Graph saved successfully",
"status": True
})
当我尝试update_or_create时,它会给出以下错误
追踪(最近一次通话): 文件" /home/nikhil/general-env/local/lib/python2.7/site-packages/django/core/handlers/exception.py" ;,第41行,内在 response = get_response(request)
文件" /home/nikhil/general-env/local/lib/python2.7/site-packages/django/core/handlers/base.py" ;,第187行,在_get_response中 response = self.process_exception_by_middleware(e,request)
文件" /home/nikhil/general-env/local/lib/python2.7/site-packages/django/core/handlers/base.py" ;,第185行,在_get_response中 response = wrapped_callback(request,* callback_args,** callback_kwargs)
File" /home/nikhil/Projects/myProject/myProject/utils/decorators.py" ;,第46行,换行 返回函数(request,* args,** kwargs)
文件" /home/nikhil/Projects/myProject/myProject/SensorMonitor/views.py" ;,第127行,在save_graph中 "传感器":传感器
文件" /home/nikhil/general-env/local/lib/python2.7/site-packages/django/db/models/manager.py",第85行,在manager_method中 return getattr(self.get_queryset(),name)(* args,** kwargs)
文件" /home/nikhil/general-env/local/lib/python2.7/site-packages/django/db/models/query.py",第476行,在update_or_create中 lookup,params = self._extract_model_params(默认值,** kwargs)
文件" /home/nikhil/general-env/local/lib/python2.7/site-packages/django/db/models/query.py",第534行,在_extract_model_params中 "','" .join(已排序(invalid_params)),
FieldError:模型图表的字段名称无效:' created_by',' query',' sensor',' server_details'