所以我有这个从MySQL基础生成的模型:
class Problem(models.Model):
idproblem = models.IntegerField(db_column='idProblem', primary_key=True)
iduser = models.ForeignKey('User', db_column='idUser')
md5hash = models.CharField(db_column='md5Hash', max_length=32, blank=True)
dateproblem = models.DateTimeField(db_column='dateProblem', blank=True, null=True)
doneproblem = models.IntegerField(db_column='doneProblem', blank=True, null=True)
answerproblem = models.CharField(db_column='answerProblem', max_length=40, blank=True)
datedoneproblem = models.DateTimeField(db_column='dateDoneProblem', blank=True, null=True)
idsubproblem = models.IntegerField(db_column='idSubProblem', blank=True, null=True)
class Meta:
managed = False
db_table = 'problem'
class Subproblem(models.Model):
idproblem = models.ForeignKey(Problem, db_column='idProblem')
idsubproblem = models.IntegerField(db_column='idSubProblem')
stringsubproblem = models.CharField(db_column='stringSubProblem', max_length=8, blank=True)
iduser = models.ForeignKey('User', db_column='idUser', blank=True, null=True)
senddateproblem = models.DateTimeField(db_column='sendDateProblem', blank=True, null=True)
receivedateproblem = models.DateTimeField(db_column='receiveDateProblem', blank=True, null=True)
idstatus = models.ForeignKey(Status, db_column='idStatus')
class Meta:
managed = False
db_table = 'subproblem'
然后我有了这个View,它负责过滤问题和SubProblem:
def pieceSender(request):
#When I filter the Problem Model ther is no problem
problems = Problem.objects.filter(doneproblem = False)
problem = problems[0]
# But when I try to do the same to the Subproblem Model the error below occours
subproblems = Subproblem.objects.filter(idproblem = problem)
print subproblems[0].idsubproblem
# This is just here So that I can see the error on the Browser
dados = {}
dados['status'] = 'OK'
data_json = simplejson.dumps(dados)
return HttpResponse(data_json)
这是提到的错误
OperationalError at /receivePiece/
(1054, "Unknown column 'subproblem.id' in 'field list'")
Request Method: GET
Request URL: http://localhost:8000/receivePiece/
Django Version: 1.6.2
Exception Type: OperationalError
Exception Value: (1054, "Unknown column 'subproblem.id' in 'field list'")
Exception Location: C:\Python27\lib\site-packages\MySQLdb\connections.py in defaulterrorhandler, line 36
Python Executable: C:\Python27\python.exe
Python Version: 2.7.6
Python Path:
['D:\\Projetos\\sgpd',
'C:\\Windows\\system32\\python27.zip',
'C:\\Python27\\DLLs',
'C:\\Python27\\lib',
'C:\\Python27\\lib\\plat-win',
'C:\\Python27\\lib\\lib-tk',
'C:\\Python27',
'C:\\Python27\\lib\\site-packages']
Server time: Thu, 17 Apr 2014 02:36:18 -0300
这是否会导致SubProblem Model没有PrimaryKey?
如果是这样,我如何让Django认识到这个表是Double PrimaryKey表?
任何帮助都非常感激! = d
答案 0 :(得分:2)
是的,这是因为你没有在子问题模型中指定一个主键,而Django正在期待一个主键。要解决此问题,请删除" managed"来自Meta并执行" django-admin syncdb",现在一切都应该有效。
答案 1 :(得分:0)
我认为这是因为你的模型没有主键 - Django假设你有一个名为id的pk列(自动生成)你可以查看这个应用程序:https://github.com/simone/django-compositekey - 它会允许您定义复合pk。