简体   繁体   English

DRF ForeignKey值未部分显示

[英]DRF ForeignKey value not showing partially

I am newbie in python django, just couple weeks. 我是python django的新手,仅几个星期。 And this is my first question post. 这是我的第一个问题。

I found problems using DRF ForeignKey. 我在使用DRF ForeignKey时发现了问题。 The foreign key value (name) do not showing up. 外键值(名称)不显示。 I have 3 tables which are related each other (Brand, Country, BrandCountry). 我有3个彼此相关的表(品牌,国家/地区,BrandCountry)。 After followed some tutorials, in BrandCountry Entity, I succeed print out the name of brand (instead of ID), but it is not working with the same code structure for country relation. 在遵循了一些教程之后,在BrandCountry Entity中,我成功打印出了品牌名称(而不是ID),但是该名称不适用于相同的国家/地区代码结构。 country name not showing 国家名称未显示

I wonder what is the problem causing this. 我不知道是什么原因造成的。 There is no error or warning message. 没有错误或警告消息。 I tried to debug, the query run correctly (it selects brand.name and country.name correctly). 我尝试调试,查询正确运行(它正确选择了brand.name和country.name)。 correct query from debug 从调试正确查询

I wonder is it because of : - I use 'code' for relation key instead of 'ID'? 我不知道这是由于以下原因:-我将“代码”用作关系键而不是“ ID”吗?

  • the name of the Country contains Kanji(special character)? 国家名称中是否包含汉字(特殊字符)?

  • or is there something i miss? 还是我想念什么?

Models.py 型号

class BrandCountry(models.Model):
    class Meta:
        db_table = 'brand_countries'

    brand = models.ForeignKey(to='brand.Brand', on_delete=models.CASCADE, related_name='ref_brand_country_brand')
    country_code = models.ForeignKey(to='country.Country',
                                    on_delete=models.CASCADE,
                                    related_name='ref_brand_country_country',
                                    max_length=45,
                                    db_column='country_code',
                                    to_field='code')

Serializers.py Serializers.py

class BrandCountrySerializer(serializers.ModelSerializer):
    brand_name = serializers.StringRelatedField(source='brand.name')
    country_name = serializers.StringRelatedField(source='country.name')

    class Meta:
        model = BrandCountry
        fields = ('id', 'brand_id', 'brand_name', 'country_code', 'country_name')

    @staticmethod
    def setup_eager_loading(queryset):
        queryset = queryset.select_related('brand','country_code')
        return queryset

Views.py Views.py

class BrandCountryListView(generics.ListAPIView):
    serializer_class = BrandCountrySerializer
    brand_id = "brand_id"

    def get_queryset(self):
        serializer_class = BrandCountrySerializer.setup_eager_loading(BrandCountry.objects)
        queryset = serializer_class.all()
        brand_id = self.kwargs.get(self.brand_id)
        queryset = queryset.filter(brand_id=brand_id)

        return queryset

I tried to debug and find the solution here and there almost spend more than 1 days, but no result. 我尝试调试并在此处找到解决方案,几乎花了超过1天的时间,但没有结果。 If there someone can help me, I really appreciate it. 如果有人可以帮助我,我真的很感激。 Sorry if something is missing or unclear. 抱歉,如果缺少或不清楚。

You are using wrong refference for country_name in serializer. 您在序列化程序中country_name使用了错误的引用 So change source='country.name' to source='country_code.name' 因此,将source='country.name'更改为source='country_code.name'

class BrandCountrySerializer(serializers.ModelSerializer):
    brand_name = serializers.StringRelatedField(source='brand.name')
    country_name = serializers.StringRelatedField(source='country_code.name') # Change is here <<<<

    class Meta:
        model = BrandCountry
        fields = ('id', 'brand_id', 'brand_name', 'country_code', 'country_name')

    @staticmethod
    def setup_eager_loading(queryset):
        queryset = queryset.select_related('brand','country_code')
        return queryset



UPDATE 更新
You can try depth attribute, as: 您可以尝试depth属性,如下所示:

class BrandCountrySerializer(serializers.ModelSerializer):
    class Meta:
        model = BrandCountry
        fields = '__all__'
        depth = 1

    @staticmethod
    def setup_eager_loading(queryset):
        queryset = queryset.select_related('brand', 'country_code')
        return queryset

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM