Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 参考2个表的模型字段_Python_Django_Django Models_Django Admin - Fatal编程技术网

Python 参考2个表的模型字段

Python 参考2个表的模型字段,python,django,django-models,django-admin,Python,Django,Django Models,Django Admin,我正在试着定制 我希望它可以为每个问题和每个答案添加图像。但有些人也不会有任何图像 这是实际模型: class Question(models.Model): quiz = models.ManyToManyField(Quiz, blank=True, ) category = models.ForeignKey(Category, blank=True, null=True, ) content = models.CharField(max_length=100

我正在试着定制

我希望它可以为每个问题和每个答案添加图像。但有些人也不会有任何图像

这是实际模型:

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
在这里,我得到了这个解决方案:

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字段?如果它应该引用另外两个表(问题或答案),那么我不知道该怎么做。

Django不支持复合主键。检查答案:谢谢,这真的很有帮助