我在填充数据库时遇到问题。 我试图坚持使用Django教程,但它不起作用。 我错过了一些东西,但我不知道:(是什么。
我有以下模型:
from django.db import models
class QA_machine_DB(models.Model):
QAmachine = models.CharField( max_length = 64)
status = models.CharField(max_length=32)
def __str__(self):
return "%s %s" % (self.QAmachine, self.status)
class Report(models.Model):
store = models.CharField(max_length= 128)
service = models.CharField(max_length= 128)
user = models.CharField(max_length= 64)
dump_date = models.CharField(max_length= 128)
branch= models.CharField(max_length= 128)
FK_report=models.ForeignKey(QA_machine_DB, null=True, blank=True, on_delete=models.SET_NULL)
def __str__(self):
return "%s %s" % (self.store, self.user)
以下代码用于填充数据库:
def import_data_django(self):
# import Data to Django
remote_data= self.report.to_dict('index')
current_machine = QA_machine_DB.objects.get_or_create( QAmachine =self.name,
status = self.status,)
for key in remote_data.keys():
Report.objects.get_or_create(service= remote_data[key]['service_name'],
user=remote_data[key]['user'],
store= remote_data[key]['shop'],
dump_date=remote_data[key]['date'],
branch = remote_data[key]['issues_key'],
FK_report= current_machine) # I guess here is the problem
print ( self.name, '-',self.status, 'in DB :)' )
if __name__ == '__main__':
machines=['qa0668']
#machines=['qa0668','con-qa-1010','qa1313']
for elt in machines :
machine=QA_machine_(elt)
machine.fill_info_status()
machine.find_state_QA_nmachine()
machine.import_data_django()
我收到错误:
TypeError:int(( 参数必须是字符串、类似字节的对象或数字,而不是 'QA_machine_DB' 如果有人知道我想念什么,请...提前谢谢你。
问题就在这里
current_machine = QA_machine_DB.objects.get_or_create( QAmachine =self.name,
status = self.status,)
get_or_create返回一个元组(实例,已创建(。试试这个
current_machine, _ = QA_machine_DB.objects.get_or_create( QAmachine =self.name,
status = self.status,)