Django Rest Framework序列化错误:类型类型的对象不可JSON序列化

时间:2018-11-27 12:28:17

标签: python json django

我是django和python的新手,目前正尝试使用django-rest-framework实现REST api。我知道有很多类似的问题,但是它们正在使用.json序列化对象,而我不是。

我有2个型号的船长和船,如下所示:

Exception in thread "main" java.lang.UnsatisfiedLinkError: /Users/home_pc/NetBeansProjects/JConcorde/libtsp.dylib: dlopen(/Users/home_pc/NetBeansProjects/JConcorde/libtsp.dylib, 1): no suitable image found.  Did find:
    /Users/home_pc/NetBeansProjects/JConcorde/libtsp.dylib: mach-o, but wrong filetype
    /Users/home_pc/NetBeansProjects/JConcorde/libtsp.dylib: mach-o, but wrong filetype
    at java.base/java.lang.ClassLoader$NativeLibrary.load0(Native Method)
    at java.base/java.lang.ClassLoader$NativeLibrary.load(ClassLoader.java:2430)
    at java.base/java.lang.ClassLoader$NativeLibrary.loadLibrary(ClassLoader.java:2487)
    at java.base/java.lang.ClassLoader.loadLibrary0(ClassLoader.java:2684)
    at java.base/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2649)
    at java.base/java.lang.Runtime.loadLibrary0(Runtime.java:829)
    at java.base/java.lang.System.loadLibrary(System.java:1867)
    at jconcorde.TSP.<clinit>(TSP.java:15)
/Users/home_pc/NetBeansProjects/JConcorde/nbproject/build-impl.xml:1328: The following error occurred while executing this line:
/Users/home_pc/NetBeansProjects/JConcorde/nbproject/build-impl.xml:948: Java returned: 1

如您所见,每艘船都有一名船长,而一名船长可以有多艘船。因此,在数据库中,有一个Boat表的captain_id字段。我为每个模型定义了两个序列化器:

    //models.py
    from django.db import models

    class Captain(models.Model):
        name = models.CharField(max_length=255, blank=False, unique=False)
        last_name = models.CharField(max_length=255, blank=False, unique=False)
        government_id = models.CharField(max_length=55, blank=False, unique=True)
        company_name = models.CharField(max_length=255, blank=False, unique=False)
        phone_number = models.CharField(max_length=55, blank=False, unique=False)
        tax_id = models.CharField(max_length=55, blank=False, unique=True)
        date_created = models.DateTimeField(auto_now_add=True)
        date_modified = models.DateTimeField(auto_now=True)

    class Boat(models.Model):
        captain = models.ForeignKey(Captain, on_delete=models.CASCADE, null=True, blank=True, related_name='boats')
        name = models.CharField(max_length=55, blank=False)
        journey_type = models.CharField(max_length=55, null=True, blank=True)
        category = models.CharField(max_length=55, null=True, blank=True)
        passenger_capacity = models.IntegerField()
        crew_count = models.IntegerField()
        have_ac = models.IntegerField(default=0)
        year_built = models.DateField
        year_restored = models.DateField(blank=True)
        engine = models.CharField(max_length=255, blank=True, null=True)
        generator = models.CharField(max_length=255, blank=True, null=True)
        width = models.CharField(max_length=255, null=True)
        height = models.CharField(max_length=255, null=True)
        length = models.CharField(max_length=255, null=True)
        wc_count = models.IntegerField(null=True)
        master_cabin_count = models.IntegerField(null=True)
        standart_cabin_count = models.IntegerField(blank=False, null=False)
        date_created = models.DateTimeField(auto_now_add=True)
        date_modified = models.DateTimeField(auto_now=True)

然后,我为每个模型定义了视图。         //views.py         从django.shortcuts导入渲染         从rest_framework导入泛型         从.serializers导入CaptainSerializer,BoatSerializer         从.models进口船长,船

    //serializers.py
    from rest_framework import serializers
    from .models import Captain, Boat

    class CaptainSerializer(serializers.ModelSerializer):
        class Meta:
            model = Captain
            fields = ('id', 'name', 'last_name', 'government_id', 'company_name', 'phone_number', 'tax_id', 'date_created', 'date_modified','boats')

    class BoatSerializer(serializers.ModelSerializer):
        captain = serializers.PrimaryKeyRelatedField(many=False, read_only=True)

        class Meta:
            model = Boat
            fields = ('id', 'captain', 'name', 'journey_type', 'category', 'passenger_capacity', 'crew_count', 'have_ac', 'year_built', 'year_restored', 'wc_count', 'standart_cabin_count')

当我转到localhost:port / captains /(或邮递员的/ GET / captains)页面时,我可以拥有船长模型,包括他拥有的船。

但是问题是:当我转到localhost:port / boats(或邮递员)时,会给出

类型为type的对象不可JSON序列化

异常类型:/ boats /处的TypeError 异常值:类型类型的对象不可JSON序列化

任何想法我该如何解决?预先感谢。

1 个答案:

答案 0 :(得分:1)

我认为year_built模型中的Boat字段定义不正确(您要分配类DateField而不是创建它的实例):

class Boat(models.Model):
    year_built = models.DateField  # should be models.DateField()