2016-08-03 62 views
-1

我不能讓我的序列化程序在外鍵字段上正常工作任何幫助將不勝感激。與json數據在Django的自定義序列化程序問題

以下是錯誤

dim_json = DimensionSerializer(dim_data)

打印dim_json

DimensionSerializer([,,,,,):

id = IntegerField(label='ID', read_only=True) 

    description = CharField(max_length=255) 

    style = CharField(max_length=255) 

    created_at = DateField() 

    updated_at = DateField() 

    target = IntegerField() 

    upper_limit = IntegerField() 

    lower_limit = IntegerField() 

    inspection_tool = CharField(max_length=255) 

    critical = IntegerField() 

    units = CharField(max_length=255) 

    metric = CharField(max_length=255) 

    target_strings = CharField(max_length=255) 

    ref_dim_id = IntegerField() 

    nested_number = IntegerField() 

    met_upper = IntegerField() 

    met_lower = IntegerField() 

    valc = CharField(max_length=255) 

    sheet = PrimaryKeyRelatedField(queryset=Sheet.objects.all(), required=False) 

serializers.py

from rest_framework import serializers 
from app.models import Dimension, Sheet, Customer 

class DimensionSerializer(serializers.ModelSerializer): 
    description = serializers.CharField(max_length=255) 
    style = serializers.CharField(max_length=255) 
    created_at = serializers.DateField() 
    updated_at = serializers.DateField() 
    target = serializers.IntegerField() 
    upper_limit = serializers.IntegerField() 
    lower_limit = serializers.IntegerField() 
    inspection_tool = serializers.CharField(max_length=255) 
    critical = serializers.IntegerField() 
    units = serializers.CharField(max_length=255) 
    metric = serializers.CharField(max_length=255) 
    target_strings = serializers.CharField(max_length=255) 
    ref_dim_id = serializers.IntegerField() 
    nested_number = serializers.IntegerField() 
    #position = serializers.IntegerField() 
    met_upper = serializers.IntegerField() 
    met_lower = serializers.IntegerField() 
    valc = serializers.CharField(max_length=255) 
    #if found this with a google search but still does not work and continues to give error. ..... 
    sheet = SheetSerializer(many=True) 
    class Meta: 
    model = Dimension 
    read_only_fields = ('id', 'created_at', 'updated_at', 'posistion', sheet) 



class SheetSerializer(serializers.ModelSerializer): 
    create_date = serializers.DateField() 
    updated_date = serializers.DateField() 
    customer_name = serializers.CharField(max_length=255) 
    part_number = serializers.CharField(max_length=255) 
    part_revision = serializers.CharField(max_length=255) 
    work_order = serializers.CharField(max_length=255) 
    purchase_order = serializers.CharField(max_length=255) 
    sample_size = serializers.IntegerField() 
    sample_scheme = serializers.CharField(max_length=255) 
    overide_scheme = serializers.IntegerField() 
    template = serializers.IntegerField() 
    sample_schem_percent = serializers.IntegerField() 
    critical_dimensions = serializers.IntegerField() 
    closed = serializers.IntegerField() 
    serial_index = serializers.CharField(max_length=255) 
    drawing_number = serializers.CharField(max_length=255) 
    drawing_revision = serializers.CharField(max_length=255) 
    heat_number = serializers.CharField(max_length=255) 
    note = serializers.CharField(max_length=255) 
    valc = serializers.CharField(max_length=255) 
    customer = CustomerSerializer(many=True) 
    class Meta: 
    model = Sheet 
    read_only_fields = ('id', 'create_date', 'updated_at') 


class CustomerSerializer(serializers.ModelSerializer): 
    customer_id = serializers.IntegerField() 
    customer_name = serializers.CharField(max_length=255) 
    created_at = serializers.DateField() 
    updated_at = serializers.DateField() 

models.py

class Dimension(models.Model): 
    description = models.CharField(max_length=255) 
    style = models.CharField(max_length=255) 
    created_at = models.DateField() 
    updated_at = models.DateField() 
    target = models.IntegerField() 
    upper_limit = models.IntegerField() 
    lower_limit = models.IntegerField() 
    inspection_tool = models.CharField(max_length=255) 
    critical = models.IntegerField() 
    units = models.CharField(max_length=255) 
    metric = models.CharField(max_length=255) 
    target_strings = models.CharField(max_length=255) 
    ref_dim_id = models.IntegerField() 
    nested_number = models.IntegerField() 

    met_upper = models.IntegerField() 
    met_lower = models.IntegerField() 
    valc = models.CharField(max_length=255) 
    sheet = models.ForeignKey(Sheet, on_delete=models.CASCADE, default=DEFAULT_FOREIGN_KEY) 


class Customer(models.Model): 
    objects = CustomerManager() 
    customer_id = models.IntegerField() 
    customer_name = models.CharField(max_length=255) 
    created_at = models.DateField 
    updated_at = models.DateField 


class Sheet(models.Model): 
    objects = SheetManager() 

    create_date = models.DateField() 
    updated_date = models.DateField() 
    customer_name = models.CharField(max_length=255) 
    part_number = models.CharField(max_length=255) 
    part_revision = models.CharField(max_length=255) 
    work_order = models.CharField(max_length=255) 
    purchase_order = models.CharField(max_length=255) 
    sample_size = models.IntegerField() 
    sample_scheme = models.CharField(max_length=255) 
    overide_scheme = models.IntegerField() 
    template = models.IntegerField() 
    sample_schem_percent = models.IntegerField() 
    critical_dimensions = models.IntegerField() 
    closed = models.IntegerField() 
    serial_index = models.CharField(max_length=255) 
    drawing_number = models.CharField(max_length=255) 
    drawing_revision = models.CharField(max_length=255) 
    heat_number = models.CharField(max_length=255) 
    note = models.CharField(max_length=255) 
    valc = models.CharField(max_length=255) 
    customer = models.ForeignKey(Customer, on_delete=models.CASCADE, default=DEFAULT_FOREIGN_KEY) 
+0

請更改格式。 – giosans

+1

@giosans完成 – Snowman08

+1

@雪人08看你的問題頂部的變化太請了 – YPCrumble

回答

0

我想通了,而不是用我的自定義序列我只是用django.core一個和急它的工作原理。

dim_json = serializer.serialize('json', Dimension.objects.all()) 

print dim_json