我有一个自定义管理器,可以从POST请求创建一个对象。问题是2多个参数都没有传递任何信息。
以下是方法(在自定义管理器中)
class ConditionManager(models.Manager):
use_in_migrations = True
use_for_related_fields=True
def create_condition(self, *args, **kwargs):
print(args)
for i in kwargs:
print i, kwargs[i]
condition_obj = conditions.models.Condition(name=kwargs['name'], contagious=kwargs['contagious'], treatable=kwargs['treatable'], treatment=kwargs['treatment'], severeity=kwargs['severeity'], symptoms=kwargs['symptoms'], speciality=kwargs['speciality'])
condition_obj.save()
return condition_obj
这是序列化程序
class ConditionSerializer(serializers.ModelSerializer):
symptoms = SymptomSerializer(read_only=True, many=True)
def create(self, validated_data):
attrs = validated_data
request = self.context['request']
return Condition.new.create_condition(**attrs)
class Meta:
model = Condition
fields = ('id', 'treatment', 'name', 'contagious', 'treatable', 'treatment', 'severeity', 'symptoms')
这是POST请求
这是日志
() 可以治疗的 名字癫痫 治疗1 传染性错误 严重7 内部服务器错误:/ conditions / new / condition / Traceback(最近一次调用最后一次): 文件" /Library/Python/2.7/site-packages/django/core/handlers/exception.py" ;,第39行,内在 response = get_response(request) 文件" /Library/Python/2.7/site-packages/django/core/handlers/base.py" ;,第187行,在_get_response中 response = self.process_exception_by_middleware(e,request) 文件" /Library/Python/2.7/site-packages/django/core/handlers/base.py" ;,第185行,在_get_response中 response = wrapped_callback(request,* callback_args,** callback_kwargs) 文件" /Library/Python/2.7/site-packages/django/views/decorators/csrf.py",第58行,在wrapped_view中 return view_func(* args,** kwargs) 文件" /Library/Python/2.7/site-packages/django/views/generic/base.py" ;,第68行,在视图中 return self.dispatch(request,* args,** kwargs) 文件" /Library/Python/2.7/site-packages/rest_framework/views.py" ;,第477行,发送 response = self.handle_exception(exc) 在handle_exception中的文件" /Library/Python/2.7/site-packages/rest_framework/views.py",第437行 self.raise_uncaught_exception(EXC) 文件" /Library/Python/2.7/site-packages/rest_framework/views.py" ;,第474行,发送 response = handler(request,* args,** kwargs) 文件" /Library/Python/2.7/site-packages/rest_framework/decorators.py",第52行,处理程序 return func(* args,** kwargs) 文件" /Users/prashantcraju/medicalrecords/conditions/views.py",第20行,在create_condition中 serializer.save() File" /Library/Python/2.7/site-packages/rest_framework/serializers.py" ;,第214行,保存 self.instance = self.create(validated_data) File" /Users/prashantcraju/medicalrecords/conditions/serializers.py" ;,第21行,在创建 return Condition.new.create_condition(** attrs) 文件" /Users/prashantcraju/medicalrecords/conditions/managers.py",第20行,在create_condition中 condition_obj = conditions.models.Condition(name = kwargs [' name'],contagious = kwargs ['传染性'],可治疗= kwargs ['可治疗'],治疗= kwargs ['治疗'],严重性= kwargs ['严重性'],症状= kwargs ['症状'],专业= kwargs ['专业']) KeyError:'症状' [19 / Feb / 2017 20:51:11]" POST / conditions / new / condition / HTTP / 1.1" 500 14989
正如你所看到的,2多个字段甚至都没有显示在日志中。