如何从多个嵌套的序列化器获取数据?

时间:2019-03-22 05:43:07

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

序列化器

public class MainActivity extends AppCompatActivity {
    View white;
    Animation downtoup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Remove notification bar
        View decorView = getWindow().getDecorView();
        int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
        decorView.setSystemUiVisibility(uiOptions);

        //MovingScanSquare
        white = findViewById(R.id.white);
        downtoup = AnimationUtils.loadAnimation(this, R.anim.downtoup);

        white.setVisibility(View.VISIBLE);
        downtoup = new TranslateAnimation(
                TranslateAnimation.ABSOLUTE, 0f,
                TranslateAnimation.ABSOLUTE, 0f,
                TranslateAnimation.RELATIVE_TO_PARENT, 1.1f,
                TranslateAnimation.RELATIVE_TO_PARENT, 0.f);
        downtoup.setDuration(2200);
        downtoup.setRepeatCount(-1);
        downtoup.setRepeatMode(Animation.REVERSE);
        downtoup.setInterpolator(new LinearInterpolator());
        white.setAnimation(downtoup);


        //movingscreen
        final ImageView backgroundOne =  findViewById(R.id.background_one);
        final ImageView backgroundTwo =  findViewById(R.id.background_two);

        final ValueAnimator animator = ValueAnimator.ofFloat(0.0f, -1.0f);
        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.setInterpolator(new LinearInterpolator());
        animator.setDuration(3000L);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                final float progress = (float) animation.getAnimatedValue();
                final float width = backgroundOne.getWidth();
                final float translationX = width * progress;
                backgroundOne.setTranslationX(translationX);
                backgroundTwo.setTranslationX(translationX + width);
            }
        });
        animator.start();
    }
}

查看文件

class CarrGetOrderDetails(serializers.Serializer):
    order = CarrGetOrderDetail(required=False)
    template = CarrGetOrderDetailTemplate(required=False, many=True)
    extra_fields = CarrGetOrderDetailExtraFields(required=False, many=True)

在这里,我尝试击中多个序列化程序对象,但在serializer.data中得到空数据

输出

class CarrierOrderDetails(APIView):
    permission_classes = (IsAuthenticated,)

    def get(self, request):
        order_id = request.GET['order_id']
        #order_obj = Order.objects.filter(id=order_id)

        obj = self.get_objects(order_id)
        #print('#####',obj)
        serializ = CarrGetOrderDetails(obj, many=True)
        return Response(serializ.data)

    def get_objects(self, order_obj):
        model1 = Order.objects.filter(id=order_obj)
        model2 = OrderTemplate.objects.filter(id=1)
        model3 = OrderExtraField.objects.filter(id=1)
        obj = {'order': model1, 'template': model2, 'extra_fields': model3}
        return obj

1 个答案:

答案 0 :(得分:1)

您正在使用<input type="email" style={{zIndex:":-100", position:"absolute"}}/> <input type="password" style={{zIndex:"-100", position:"absolute"}}/> 初始化序列化程序,因此它需要对象列表。您应该将对象列表作为第一个参数传递给序列化程序类构造函数,或者删除many=True kwarg。

此外,由于您要传入字典,因此我将使用many=True kwarg而不是第一个参数data={...}

作为快速测试,您可以执行以下修改:

instance
obj = self.get_objects(order_id)
serializ = CarrGetOrderDetails(obj, many = True)

参考