我对Django比较新,但我试图为以下数据模型返回完整的JSON文档。
class Wheels(models.Model):
w_name = models.CharField(max_length=255)
w_weight = models.IntegerField(default=200)
w_size = models.CharField(max_length=200)
w_durability = models.IntegerField(default=45)
def __str__(self):
return self.w_name
class Chassis(models.Model):
c_owner = models.CharField(max_length=255)
c_year = models.IntegerField(default=1900)
c_model = models.CharField(max_length=255)
c_wheels = models.ForeignKey(Wheels)
c_weight = models.IntegerField(default=20)
def __str__(self):
return self.c_model`
现在代码只返回:
{
"id": 1,
"c_owner": "Mike",
"c_year": 1969,
"c_model": "Chevy",
"c_weight": 3500,
"c_wheels": 1
}
但是我不想用轮子来代替轮子表
查看:
class ChassisList(ModelViewSet):
queryset = Chassis.objects.all()
serializer_class = ChassisSerializer
class WheelsList(ModelViewSet):
queryset = Wheels.objects.all()
serializer_class = WheelSerializer
串行器:
class WheelSerializer(serializers.ModelSerializer):
class Meta:
model = Wheels
fields = ('__all__')
class ChassisSerializer(serializers.ModelSerializer):
class Meta:
model = Chassis
fields = ('__all__')
答案 0 :(得分:0)
简单的解决方案:
class ChassisSerializer(serializers.ModelSerializer):
class Meta:
model = Chassis
fields = ('__all__')
depth = 1