Django Rest Framework 无法添加嵌套对象

Django Rest Framework can't add nested obejct

我是 drf 的新手,我正在尝试显示嵌套对象并同时在 ListCreateView 中具有选择功能

models.py

class CarBrand(SoftDeletionModel):
    CAR_BRAND_NAME_MAX_LEN = 30
    name = models.CharField(
        max_length=CAR_BRAND_NAME_MAX_LEN,
    )

    created_at = models.DateTimeField(
        auto_now_add=True,
    )

    def __str__(self):
        return self.name

class CarModel(SoftDeletionModel):
    CAR_MODEL_NAME_MAX_LEN = 30
    name = models.CharField(
        max_length=CAR_MODEL_NAME_MAX_LEN,
    )
    car_brand = models.ForeignKey(
       CarBrand,
       on_delete=models.CASCADE,
    )
    created_at = models.DateTimeField(
        auto_now_add=True,
    )
    updated_at = models.DateTimeField(
        auto_now=True,
    )

我的逻辑是先有汽车品牌,然后在创建新车型时指定现有汽车品牌

serializers.py

class FullCarBrandSerializer(serializers.ModelSerializer):
    class Meta:
        model = CarBrand
        fields = ('id', 'name', 'created_at')


class IdAndNameCarBrandSerializer(serializers.ModelSerializer):
    class Meta:
        model = CarBrand
        fields = ('id', 'name')


class FullCarModelSerializer(serializers.ModelSerializer):
    car_brand = IdAndNameCarBrandSerializer(many=False)

    class Meta:
        model = CarModel
        fields = ('id', 'name', 'created_at', 'updated_at', 'car_brand')

当我没有 car_brand = IdAndNameCarBrandSerializer(many=False) 汽车品牌选择的创建部分工作正常 correct_choices_img, but that's not the way I want to display the JSON incorrect_nested_field_img(it shows only the id, but I want id and name) however when I add that same line again I get what I want in the JSON which is like this correct_nested_field_img, but the functionality of choosing exciting car brands goes away incorrect_choices_img 我认为它希望我用它创建一个新品牌,但我不是想要

views.py

class CarModelListCreateView(api_views.ListCreateAPIView):
     queryset = CarModel.objects.all()
     serializer_class = FullCarModelSerializer

问题 显示嵌套对象的正确方法是什么,并具有带有选项的创建功能?

我对 django 不是很好,但我正在尝试回答这个问题。

要做到这一点,您只需:

将一个 class 添加到您的 serializers.py

serializers.py

...

class FullCarModelSerializer(serializers.ModelSerializer):
    car_brand = IdAndNameCarBrandSerializer(many=False)

    class Meta:
        model = CarModel
        fields = ('id', 'name', 'created_at', 'updated_at', 'car_brand')

class CreateCarModelSerializer(serializers.ModelSerializer):

    class Meta:
        model = CarModel
        fields = ('id', 'name', 'car_brand')

向现有视图添加一种方法 class。

views.py

class CarModelListCreateView(api_views.ListCreateAPIView):
     queryset = CarModel.objects.all()
     serializer_class = FullCarModelSerializer

    def get_serializer_class(self):
        if self.request.method == 'POST':
            return CreateCarModelSerializer
        return FullCarModelSerializer

您可以参考 Django rest framework 中的 api 个指南。

serializer_class - The serializer class that should be used for validating and deserializing input, and for serializing output. Typically, you must either set this attribute, or override the get_serializer_class() method.