1

我有一个从 POST 请求创建对象的自定义管理器。问题是这 2 个 manytomany 参数没有传递任何信息。

这是方法(在自定义管理器中)

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 请求

POST 请求

这是日志

() 可治疗 真名癫痫治疗 1 传染性 假严重性 7 内部服务器错误:/conditions/new/condition/ Traceback(最近调用最后一次):文件“/Library/Python/2.7/site-packages/django/core/handlers/ exception.py”,第 39 行,内部响应 = get_response(request) 文件“/Library/Python/2.7/site-packages/django/core/handlers/base.py”,第 187 行,在 _get_response response = self.process_exception_by_middleware (e,请求)文件“/Library/Python/2.7/site-packages/django/core/handlers/base.py”,第185行,在_get_response响应=wrapped_callback(request,*callback_args,**callback_kwargs)文件“/ Library/Python/2.7/site-packages/django/views/decorators/csrf.py”,第 58 行,在 Wrapped_view 返回 view_func(*args, **kwargs) 文件“/Library/Python/2.7/site-packages/django/views/generic/base.py”,第 68 行,在视图中返回 self.dispatch(request, *args, **kwargs) 文件“/Library/Python/2.7/ site-packages/rest_framework/views.py",第 477 行,在 dispatch response = self.handle_exception(exc) File "/Library/Python/2.7/site-packages/rest_framework/views.py",第 437 行,在handle_exception self .raise_uncaught_exception(exc) 文件“/Library/Python/2.7/site-packages/rest_framework/views.py”,第 474 行,调度响应 = handler(request, *args, **kwargs) 文件“/Library/Python/ 2.7/site-packages/rest_framework/decorators.py”,第 52 行,在处理程序返回 func(*args, **kwargs) 文件“/Users/prashantcraju/medicalrecords/conditions/views.py”,第 20 行,在 create_condition 序列化程序中.save() 文件“/Library/Python/2.7/site-packages/rest_framework/serializers.py”,第 214 行,保存 self.instance = self.create(validated_data) 文件“/Users/prashantcraju/medicalrecords/conditions/serializers .py",第 21 行,在 create_condition_obj = conditions.models.Condition(name =kwargs['name'],传染性=kwargs['contagious'],可治疗=kwargs['可治疗'],治疗=kwargs['治疗'],严重性=kwargs['严重性'],症状=kwargs['症状'], speciality=kwargs['speciality']) KeyError: 'symptoms' [19/Feb/2017 20:51:11] "POST /conditions/new/condition/HTTP/1.1"500 14989

如您所见,2 manytomany 字段甚至没有出现在日志中。

4

0 回答 0