所以我有两个模型;第一个用于帖子和第二张照片。每个帖子可以有很多照片。&我不确定如何在 django 中构建一对多关系。
这是我的模型:
class Post(models.Model):
title = models.CharField(max_length=200)
description = models.TextField()
pub_date = models.DateTimeField('date published')
class Photo(models.Model):
FILE_TYPE_CHOICES = (
('full', 'Full Width'),
('half', 'Half Width')
)
title = models.CharField(max_length=200)
description = models.TextField()
photoType = models.CharField(max_length=16,choices = FILE_TYPE_CHOICES, default = 'full', blank = True)
imageFile = models.ImageField(upload_to='uploaded')
containedPost = models.ForeignKey('Post', related_name='photoPosts')
我怎样才能建立适当的关系,我怎样才能访问模板中的帖子和每张照片?