Python 如何使用TastyPie在RESTAPI中获取外键值?
您好,我正在将Python 如何使用TastyPie在RESTAPI中获取外键值?,python,mysql,django,tastypie,Python,Mysql,Django,Tastypie,您好,我正在将tastype与我的Django项目一起使用。当我在resourceapi中获取与其他模型有外键关系的模型值时,它返回空值 我使用MySql作为我的数据库 我的型号代码: TopicDetailModel class Topicdetails(models.Model): apptopicid = models.IntegerField(db_column='appTopicID', primary_key=True) # Field name made lowercase
tastype
与我的Django
项目一起使用。当我在resourceapi
中获取与其他模型有外键关系的模型值时,它返回空值
我使用MySql作为我的数据库
我的型号代码:
TopicDetail
Model
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'
讲师详细信息
型号
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.py inspectdb
命令从MySql数据库创建了模型文件
请帮助我……将
null=True
设置为允许空值
topicdetails = fields.ForeignKey(TopicdetailsResource,'topicdetails',null=True)
instructordetails = fields.ForeignKey(InstructordetailsResource,'instructordetails',null=True)
如果我根据您的建议添加或修改代码,它会将所有对象返回为null。但我的数据库有价值。对于修改对象后的示例输出:[{instructordetails:null,topicdetails:null,topicinstructorrelationid:1},]如果我的外键引用为null,我不会得到
,这意味着我提到了我的问题“TopicInstructor”不会包含任何null字段。@Nagarajan我想你将null=True仅用于讲师详细信息,try只需要从中获取两个apptopicid
formtopicdetails
model和appinstructorid
instructor详细信息
model。对于此输出,我必须在代码中进行哪些更改