0

我有一个项目模型:

class Project(models.Model):
     category = models.ManyToManyField(ProjectCategory)
     designer = models.ForeignKey(User)
     name = models.CharField(max_length=255)
     description = models.TextField()
     image1 = ImageField(upload_to='.../designs/')
     image2 = ImageField(upload_to='.../designs/')
     image3 = ImageField(upload_to='.../designs/')

我想将图像分开到另一个模型:

class ProjectImages(models.Model):
     project = models.ForeignKey(Project)
     image1 = ImageField(upload_to='.../designs/')
     image2 = ImageField(upload_to='.../designs/')
     image3 = ImageField(upload_to='.../designs/')

这是一个正确的模型吗?或者将项目图像与项目区分开来的正确方法是什么?

4

3 回答 3

1

一个图像字段是可以的。您可以在每个项目中保存大量图像。

class ProjectImages(models.Model):
     project = models.ForeignKey(Project)
     image = ImageField(upload_to='.../designs/')
于 2013-03-27T11:08:05.730 回答
1

如果您希望每个项目具有 0 到 N 个图像的灵活性,那么您应该使用以下 ProjectImage 模型:

class ProjectImage(models.Model):
    project = models.ForeignKey(Project)
    image = ImageField(upload_to='.../designs/')
于 2013-03-27T11:09:20.060 回答
1

你可能想要OneToOne关系而不是Forienkey,所以模型可以是

class ProjectImages(models.Model):
     project = models.OneToOneField(Project)
     image1 = ImageField(upload_to='.../designs/')
     image2 = ImageField(upload_to='.../designs/')
     image3 = ImageField(upload_to='.../designs/')

如果每个项目的图像数量固定为 3(​​或您决定的数字),这很好。


如果您想要可变数量的图像,您可以将模型创建为

class ProjectImages(models.Model):
     project = models.ForeignKey(Project)
     image = ImageField(upload_to='.../designs/')
     sequence_num = IntegerField()

您可以添加任意数量ProjectImages并指定sequence_num给它们(如果需要)。

于 2013-03-27T11:09:46.990 回答