我有一个简单的模型,女巫用作表格。
class Test(models.Model):
name = models.CharField(max_length=100, unique=True, db_index=True)
location = models.CharField(max_length=300)
details = models.TextField()
def __unicode__(self):
return self.image.name
我想将以下类Album添加为Test的外键:
class Album(models.Model):
title = models.CharField(max_length=60)
public = models.BooleanField(default=False)
def __unicode__(self):
return self.title
class Tag(models.Model):
tag = models.CharField(max_length=50)
def __unicode__(self):
return self.tag
class Image(models.Model):
title = models.CharField(max_length=60, blank=True, null=True)
image = models.FileField(upload_to="images/")
tags = models.ManyToManyField(Tag, blank=True)
albums = models.ManyToManyField(Album, blank=True)
created = models.DateTimeField(auto_now_add=True)
rating = models.IntegerField(default=50)
width = models.IntegerField(blank=True, null=True)
height = models.IntegerField(blank=True, null=True)
user = models.ForeignKey(User, null=True, blank=True)
def __unicode__(self):
return self.image.name
问题:
如何将班级专辑作为课堂测试的外键添加?
如何将此关系放在表单上? - 例如用户正在选择多个图像进行上传,从而产生与测试类相关的唯一相册。
答案 0 :(得分:0)
对于外键来说,你的意思是这样吗
class Test(models.Model):
name = models.CharField(max_length=100, unique=True, db_index=True)
location = models.CharField(max_length=300)
details = models.TextField()
album = models.ForeignKey(Album, null=True, blank=True)
def __unicode__(self):
return self.name