我有以下AlbumImage模型,用于我的用户上传的图像:
`
class AlbumImage(models.Model):
image = ProcessedImageField(upload_to='albums', processors=[ResizeToFit(1280)], format='JPEG', options={'quality': 70})
alt = models.CharField(max_length=255, default=uuid.uuid4)
created = models.DateTimeField(auto_now_add=True)
slug = models.SlugField(max_length=70, default=uuid.uuid4, editable=False)
`
以及上述图片中可能出现的人物模型:
`
class People(models.Model):
name = models.CharField(max_length=30,default='John Doe')
remind_on = models.DateField()
event = models.TextField(default='No Event')
associated_with = models.ManyToManyField(AlbumImage)
`
问题:我希望我的用户能够从许多关联的图像中选择图像之一。如何在模型中表示出来?
答案 0 :(得分:0)
您可以按如下方式使用OneToOneField
,
class People(models.Model):
name = models.CharField(max_length=30,default='John Doe')
remind_on = models.DateField()
event = models.TextField(default='No Event')
associated_with = models.ManyToManyField(AlbumImage)
favourite_image = models.OneToOneField(AlbumImage)