Django - 在POST请求上发送JSON arrary

时间:2018-06-06 05:55:31

标签: python django django-models django-rest-framework

我正在根据数据库中可用的记录数创建动态表单。 输入字段在渲染期间动态设置。设置值后,我希望将它们批量插入到DB中。 (我正在使用modelSerializer类进行序列化)

例如

<form id="issue_form">
<input name="issue" value="random"/>
<input name="issue" value="sensor"/>
<input name="issue" value="human"/>
<input name="issue" value="pc"/>
</form>

<script>
$(docment).on('submit','#issue_form',function(event){
     event.preventDefault();
var data = $(this).serializeArray();

$.ajax({"url":"api/data",data :data }).done(function(res){
   console.log(data)
})
})

型号:

class ParetoAnalysis(models.Model):
    shift                   = models.ForeignKey(Shift, on_delete=models.CASCADE)
    line                    = models.ForeignKey(Line, on_delete=models.CASCADE)
    start_time              = models.DateTimeField(default=datetime.now)
    end_time                = models.DateTimeField(default=datetime.now)
    end_point               = models.ForeignKey(EndPoint, on_delete=models.CASCADE)
    issue                   = models.ForeignKey(DownTimeIssue, on_delete=models.CASCADE)
    custom_comments         = models.CharField(max_length=1000,blank=True)
    created_by              = models.ForeignKey(User, on_delete=models.CASCADE,null=True)
    created_date            = models.DateTimeField(default=datetime.now,blank=False)

串行:

class ParetoAnalysisSerializer(serializers.ModelSerializer):
    duration = serializers.SerializerMethodField()        #duration in seconds

    issue = serializers.PrimaryKeyRelatedField(
    read_only=False, queryset=DownTimeIssue.objects.all()) 

    line = serializers.PrimaryKeyRelatedField(
    read_only=False, queryset=Line.objects.all())

    shift = serializers.PrimaryKeyRelatedField(
    read_only=False, queryset=Shift.objects.all())

    end_point = serializers.PrimaryKeyRelatedField(
    read_only=False, queryset=EndPoint.objects.all())

    #created_by_id = 1 #serializers.SerializerMethodField('_user')
    created_by_id = serializers.SerializerMethodField()


    def get_created_by_id(self,pareto):
        return 1

    def get_duration(self, pareto):
        return  (pareto.end_time-pareto.start_time).seconds

    class Meta:
        model = ParetoAnalysis
        fields = ('custom_comments', 'end_point', 'issue','line','shift', 'created_by_id', 'created_date', 'end_time', 'start_time','duration')

视图

class ParetoAnalysisViewSet(viewsets.ModelViewSet):
    queryset = ParetoAnalysis.objects.all()
    serializer_class = ParetoAnalysisSerializer

    def create(self, request):
        serializer = self.get_serializer(data=request.data, many=True)
        serializer.is_valid(raise_exception=True)
        self.perform_create(serializer)
        headers = self.get_success_headers(serializer.data)
        return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)

我希望在对模型的post请求中存储批量JSON值。

例如

json = [{"name":"random"},{"name":"sensor"},{"name":"human"},{"name":"pc"}]

提交默认API表单时的错误日志

    return  (pareto.end_time-pareto.start_time).seconds
    AttributeError: 'collections.OrderedDict' object has no attribute 'end_time'

发送批量json时的错误日志

Traceback (most recent call last):
  File "C:\Users\5450\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
    response = get_response(request)
  File "C:\Users\5450\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\base.py", line 158, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\5450\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\base.py", line 156, in _get_response
    response = response.render()
  File "C:\Users\5450\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\response.py", line 106, in render
    self.content = self.rendered_content
  File "C:\Users\5450\AppData\Local\Programs\Python\Python36-32\lib\site-packages\rest_framework\response.py", line 72, in rendered_content
    ret = renderer.render(self.data, accepted_media_type, context)
  File "C:\Users\5450\AppData\Local\Programs\Python\Python36-32\lib\site-packages\rest_framework\renderers.py", line 718, in render
    context = self.get_context(data, accepted_media_type, renderer_context)
  File "C:\Users\5450\AppData\Local\Programs\Python\Python36-32\lib\site-packages\rest_framework\renderers.py", line 691, in get_context
    'post_form': self.get_rendered_html_form(data, view, 'POST', request),
  File "C:\Users\5450\AppData\Local\Programs\Python\Python36-32\lib\site-packages\rest_framework\renderers.py", line 496, in get_rendered_html_form
    return self.render_form_for_serializer(existing_serializer)
  File "C:\Users\5450\AppData\Local\Programs\Python\Python36-32\lib\site-packages\rest_framework\renderers.py", line 522, in render_form_for_serializer
    serializer.data,
  File "C:\Users\5450\AppData\Local\Programs\Python\Python36-32\lib\site-packages\rest_framework\serializers.py", line 742, in data
    ret = super(ListSerializer, self).data
  File "C:\Users\5450\AppData\Local\Programs\Python\Python36-32\lib\site-packages\rest_framework\serializers.py", line 266, in data
    self._data = self.get_initial()
  File "C:\Users\5450\AppData\Local\Programs\Python\Python36-32\lib\site-packages\rest_framework\serializers.py", line 577, in get_initial
    return self.to_representation(self.initial_data)
  File "C:\Users\5450\AppData\Local\Programs\Python\Python36-32\lib\site-packages\rest_framework\serializers.py", line 660, in to_representation
    self.child.to_representation(item) for item in iterable
  File "C:\Users\5450\AppData\Local\Programs\Python\Python36-32\lib\site-packages\rest_framework\serializers.py", line 660, in <listcomp>
    self.child.to_representation(item) for item in iterable
  File "E:\Personal\GM\Godrej\Mirror Code v1\report\serializers.py", line 82, in to_representation
    data =  super(ParetoAnalysisSerializer, self).to_representation(instance)
  File "C:\Users\5450\AppData\Local\Programs\Python\Python36-32\lib\site-packages\rest_framework\serializers.py", line 504, in to_representation
    ret[field.field_name] = field.to_representation(attribute)
  File "C:\Users\5450\AppData\Local\Programs\Python\Python36-32\lib\site-packages\rest_framework\relations.py", line 257, in to_representation
    return value.pk
AttributeError: 'int' object has no attribute 'pk'

1 个答案:

答案 0 :(得分:1)

您需要为此内部视图集create()方法使用序列化程序的many=True参数:

from rest_framework import status
from rest_framework.response import Response

class IssueViewSet(viewsets.ModelViewSet):
    queryset = Issue.objects.all()
    serializer_class = IssueSerializer

    def create(self, request):
        if not isinstance(request.data, dict):
            serializer = self.get_serializer(data=request.data, many=True)
        else:
            serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        self.perform_create(serializer)
        headers = self.get_success_headers(serializer.data)
        return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)