模型字段指的是2个表

时间:2014-06-30 02:28:48

标签: python django django-models django-admin

我正在尝试自定义this django quiz app

我希望能够为每个问题和每个答案添加图像。但有些人不会有任何图像,无论是答案。

这是实际模型:

class Question(models.Model):

    quiz = models.ManyToManyField(Quiz, blank=True, )

    category = models.ForeignKey(Category, blank=True, null=True, )

    content = models.CharField(max_length=1000, 
                               blank=False, 
                               help_text="Enter the question text that you want displayed",
                               verbose_name='Question',
                               )

    explanation = models.TextField(max_length=2000,
                                   blank=True,
                                   help_text="Explanation to be shown after the question has been answered.",
                                   verbose_name='Explanation',
                               )


    class Meta:
        verbose_name = "Question"
        verbose_name_plural = "Questions"
        ordering = ['category']


    def __unicode__(self):
        return self.content


class Answer(models.Model):
    question = models.ForeignKey(Question)

    content = models.CharField(max_length=1000, 
                               blank=False, 
                               help_text="Enter the answer text that you want displayed",
                               )

    correct = models.BooleanField(blank=False, 
                                  default=False,
                                  help_text="Is this a correct answer?"
                                  )

    def __unicode__(self):
        return self.content

在此question,我得到了这个解决方案:

Create new model with

image,
type - answer or question
answer_id or question_id

但我对如何创建它有一些疑问。这是我到目前为止所得到的:

class Image(models.Model):
    TYPE_CHOICES = (
        ('A','Answer'),
        ('Q','Question'),
    )
    image = models.ImageField(upload_to='static/img')
    type = models.charField(max_length=1, choices=TYPE_CHOICES)
    id = ???

    def __unicode__(self):
        return self.type

如何定义id字段?如果它应该引用其他2个表(问题或答案),那么我不知道如何做到这一点。

0 个答案:

没有答案