ForeignKey反向查询
#我的环境:主表从表一对多关系
#从表ForeignKey到主表
class PageModel(models.Model):
name = models.CharField(max_length=50, verbose_name="名字")
class ImgModel(models.Model):
f_id = models.ForigenKey(PageModel,on_delete=models.SET_NULL,null=True,verbose_name="所属页面")
img = models.FileField(upload_to="img/", verbose_name="图片")
#查询方式:
result = models.PageModel.object.get(id=1)
img_list = result.imgmodel_set.all() #关联外键表小写并加上_set