我是一個製作Q &的網站。目前,我有這樣的模型如何烘乾我的模特
class Question(models.Model):
title = models.CharField(max_length=150)
detail = models.TextField()
submitter = models.ForeignKey(User)
date_added = models.DateTimeField(auto_now_add = True)
...# some additional fields such as tags
class Answer(models.Model):
detail = models.TextField()
submitter = models.ForeignKey(User)
date_added = models.DateTimeField(auto_now_add = True)
...
class QuestionVote(models.Model):
voter = models.ForeignKey(User)
question = models.ForeignKey(Question)
#replicating what I did for QuestionVote
class AnswerVote(models.Model):
voter = models.ForeignKey(User)
question = models.ForeignKey(Question)
問題和答案模型是一樣的,除了標題和標籤。要爲Answers添加投票功能,我必須將QuestionVote模型複製爲AnswerVote,並重復我在視圖中爲問題投票所做的所有操作。我看了一下模型繼承,但如果我聲明一個抽象基類並繼承問題和答案模型,那麼我不能使用外鍵。那麼避免這種重複的最佳方法是什麼?
@誰指出了這一點,我有正確的複製粘貼錯誤 – yayu