使用Django Rest Framwork创建记录期间访问自动递增主字段值

时间:2019-02-05 12:42:33

标签: python-3.x django-rest-framework django-queryset django-serializer

我试图一次保存3个表,并且数据正在表中保存,但是我在从每个表中插入visit_id,vdata_id,vchild_id时遇到问题。 (它是具有auto_created = True设置的主键)以返回api中的值。我尝试了许多方法来实现这一目标。一种尝试是add_action('woocommerce_email_after_order_table', 'add_order_email_instructions', 10, 2); function add_order_email_instructions($order, $sent_to_admin) { if (!$sent_to_admin) { if ( isset( $gateways['cod'] ) == $order->get_payment_method()) { // cash on delivery method do_action('woocommerce_thankyou_lieferung', $order->get_id()); } else { // other methods (ie credit card) do_action('woocommerce_thankyou_lieferung', $order->get_id()); } } } ,但是它给出了类似于VisVisits.objects.latest('visit_id')的错误,共享我的代码:

序列化器

<visit_id 46> is not serializer

观看次数

class VisVisitsSerializer(serializers.ModelSerializer):

    data = VisVisitDataSerializer(many=True)
    data_child = VisVisitChildSerializer(many=True)


    class Meta:
        model = VisVisits
        read_only_fields = ['visit_id']

    def create(self, validated_data):
        visits_data = validated_data.pop('data')
        visits_child_data = validated_data.pop('data_child')

        visit = VisVisits.objects.create(**validated_data)
        #print()
        for visit_data in visits_data:
            VisVisitData.objects.create(visit=VisVisits.objects.last(), **visit_data)

        for visit__child_data in visits_child_data:
            VisVisitChild.objects.create(visit=VisVisits.objects.latest('visit_id'), **visit__child_data)
        return visit

0 个答案:

没有答案