【问题标题】:Django Restful Model Foreign Key - Specify From and To FieldDjango Restful 模型外键 - 指定 From 和 To 字段
【发布时间】:2016-05-17 19:13:07
【问题描述】:

我是 Django 新手,并试图了解如何在模型中设置数据库关系。我有以下两种型号:

class BusinessTypes(models.Model):
    categoryID = models.AutoField(db_column='TypeID', primary_key=True)  # Field name made lowercase.
    category = models.CharField(db_column='type', max_length=200)
    newtype = models.CharField(db_column='newType', max_length=200)  # Field name made lowercase.
    dec = models.CharField(max_length=255)
    tabsize = models.CharField(db_column='tabSize', max_length=10)  # Field name made lowercase.
    #subcategory = models.ForeignKey(BusinessTypesSub, to_field='Type_ID')

    class Meta:
        managed = False
        db_table = 'business_types'
        app_label = 'sapi'

class BusinessTypesSub(models.Model):
    subcategoryID = models.AutoField(db_column='Type_subID', primary_key=True)  # Field name made lowercase.
    categoryID = models.IntegerField(db_column='Type_ID')  # Field name made lowercase.
    status = models.IntegerField()
    showpub = models.IntegerField(db_column='showPub')  # Field name made lowercase.
    showcity = models.CharField(db_column='showCity', max_length=20)  # Field name made lowercase.
    subcategory = models.CharField(db_column='sub', max_length=255)
    sub_alternative = models.CharField(max_length=255)
    category = models.ForeignKey(BusinessTypes,from_field='Type_ID', to_field='TypeID')

    class Meta:
        managed = False
        db_table = 'business_types_sub'
        app_label = 'api'

business_types 和business_types_sub 表之间存在一对多的关系。我想设置一个外键关系,以便如果我创建一个通过 BusinessTypesSub 调用序列化程序的视图,我可以访问 BusinessTypesModel 中类别字段的值。外键关系应该是从 BusinessTypesSub 字段“Type_ID”到 BusinessTypes 字段“TypeID”。

我怎样才能创建这个?我尝试将以下内容添加到 BusinessTypesSub 模型中,但它只会创建以下错误:

category = models.ForeignKey(BusinessTypes,to_field='TypeID')

BusinessTypes has no field named 'TypeID'

另外,我如何在视图/序列化器中调用它。例如,我如何将“类别”添加到以下序列化程序:

class BusinessTypesSubSerializer(serializers.ModelSerializer):
    category = serializers.RelatedField(source='BusinessType', read_only=True)

    class Meta:
        model = BusinessTypesSub
        fields = ('categoryID', 'category', 'subcategoryID', 'subcategory')

【问题讨论】:

    标签: python mysql django django-models django-rest-framework


    【解决方案1】:

    你指定的to_field错误,应该是BusinessType类的categoryID字段名,而不是数据库中的列名:

    category = models.ForeignKey(BusinessTypes, to_field='categoryID')
    

    要在 BusinessTypesSubSerializer 中显示类别,您可以从此处定义的多个选项中进行选择:http://www.django-rest-framework.org/api-guide/relations/。例如。对于 StringRelatedField,您可以定义:

    class BusinessTypesSubSerializer(serializers.ModelSerializer):
        category = serializers.StringRelatedField(read_only=True)
    
        class Meta:
            model = BusinessTypesSub
            fields = ('categoryID', 'category', 'subcategoryID', 'subcategory')
    

    【讨论】:

    • 当我尝试将外键约束添加到 BusinessTypesSub 时,我收到以下错误消息:'字段列表中的未知列'business_types_sub.category_id'
    猜你喜欢
    • 1970-01-01
    • 2015-06-13
    • 1970-01-01
    • 2020-11-13
    • 2020-06-30
    • 2012-08-19
    • 2011-04-08
    • 2021-09-13
    • 2015-10-14
    相关资源
    最近更新 更多