gpt4 book ai didi

python - 如何使用 TastyPie 获取 REST Api 中的外键值?

转载 作者:行者123 更新时间:2023-11-30 00:07:56 25 4
gpt4 key购买 nike

您好,我正在将 Tastypie 与我的 Django 项目一起使用。当我获取与资源api中其他模型有外键关系的模型值时,它返回空值。

我使用 MySql 作为我的数据库。

我的模型代码:

TopicDetail 模型

class Topicdetails(models.Model):
apptopicid = models.IntegerField(db_column='appTopicID', primary_key=True) # Field name made lowercase.
portaltopicid = models.IntegerField(db_column='portalTopicID', blank=True, null=True) # Field name made lowercase.
topicname = models.CharField(db_column='topicName', max_length=500, blank=True) # Field name made lowercase.
topicshortdescription = models.CharField(db_column='topicShortDescription', max_length=5000, blank=True) # Field name made lowercase.
class Meta:
managed = False
db_table = 'TopicDetails'

讲师详细信息模型

class Instructordetails(models.Model): 
appinstructorid = models.IntegerField(db_column='appInstructorID', primary_key=True) # Field name made lowercase.
portalinstructorid = models.IntegerField(db_column='portalInstructorID') # Field name made lowercase.
firstname = models.CharField(db_column='firstName', max_length=50, blank=True) # Field name made lowercase.
middlename = models.CharField(db_column='middleName', max_length=50, blank=True) # Field name made lowercase.
lastname = models.CharField(db_column='lastName', max_length=50, blank=True) # Field name made lowercase.
optionalid = models.IntegerField(db_column='optionalID', blank=True, null=True) # Field name made lowercase.
class Meta:
managed = False
db_table = 'InstructorDetails'

TopicInstructor Model ,有来自上述两个模型的两个外键。

class Topicinstructor(models.Model):
topicinstructorrelationid = models.IntegerField(db_column='topicInstructorRelationID', primary_key=True) # Field name made lowercase.
apptopicid = models.ForeignKey(Topicdetails, db_column='appTopicID') # Field name made lowercase.
appinstructorid = models.ForeignKey(Instructordetails, db_column='appInstructorID') # Field name made lowercase.
class Meta:
managed = False
db_table = 'TopicInstructor'

还有我的 TopicInstructor api 代码:

class TopicinstructorResource(ModelResource):
# topicdetails = fields.ForeignKey(TopicdetailsResource,'topicdetails')
#instructordetails = fields.ForeignKey(InstructordetailsResource, 'instructordetails')
class Meta:
queryset = Topicinstructor.objects.all()
resource_name = 'topicinstructor'
include_resource_uri = False

如果我从我的资源 api 中取消注释以上两行代码,则会显示错误:

{
error: "The model '<Topicinstructor: Topicinstructor object>' has an empty attribute 'instructordetails' and doesn't allow a null value."
}

当获取资源时,其返回输出如下:

{
meta: {
limit: 20,
next: "/api/topicinstructor/?offset=20&limit=20&format=json",
offset: 0,
previous: null,
total_count: 994
},
objects: [
{
topicinstructorrelationid: 1
},
{
topicinstructorrelationid: 2
},
{
topicinstructorrelationid: 3
},
{
topicinstructorrelationid: 4
},
]
}

但我还需要获取外键值。更多关于我使用 python manage.pyspectdb 命令从 MySql 数据库创建了我的模型文件。

请帮助我......

最佳答案

设置null=True,使其允许空值

topicdetails = fields.ForeignKey(TopicdetailsResource,'topicdetails',null=True)
instructordetails = fields.ForeignKey(InstructordetailsResource,'instructordetails',null=True)

关于python - 如何使用 TastyPie 获取 REST Api 中的外键值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24358418/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com