我爲多選題創建了一個模型。每個問題有5個選擇 的答案。我需要每個問題對象基於它的 問題和答案是唯一的。所以,我設計這樣的模型。有沒有辦法在django中用mysql db後端創建獨特的TextField?
from django.db import models
class MultipleChoiceQuestion(models.Model):
ANSWERS = [('a', 'a'), ('b', 'b'), ('c', 'c'), ('d', 'd'), ('e', 'e')]
question = models.TextField()
a = models.TextField()
b = models.TextField()
c = models.TextField()
d = models.TextField()
e = models.TextField()
true_answer = models.CharField(max_length=1, choices=ANSWERS)
class Meta:
unique_together = [('question', 'a', 'b', 'c', 'd', 'e')]
當我運行migrate
,MySQL的給這個錯誤:
1170, "BLOB/TEXT column 'question' used in key specification without a key length"
我發現這個錯誤已經討論here。但是,我不能使用 CharField
以及它的小限制,因爲我需要存儲長文本 (直到10000個字符或更多)。
sqlite3和postgresql可以做到這一點(我的意思是django沒有抱怨關於TEXT
的關鍵規範 )。
我需要使用mysql的原因,因爲我將部署這個服務器 django應用程序只提供MySQL,沒有postgresql。
那麼,無論如何,我可以做到這一點?
所以很有幫助!謝謝 – fpghost