Skip to content

Instantly share code, notes, and snippets.

@xiaotangyuan
Created August 21, 2014 01:13
Show Gist options
  • Save xiaotangyuan/0e61f695e139faa52d6c to your computer and use it in GitHub Desktop.
Save xiaotangyuan/0e61f695e139faa52d6c to your computer and use it in GitHub Desktop.
ForeignKey的related_name用法
class ProjectStyle(models.Model):
name=models.CharField(max_length=50)
def __unicode__(self):
return self.name
class GuanZhu(models.Model):
guanzhu=models.ForeignKey(ProjectStyle)
user=models.ForeignKey(BaseInfo,related_name='guanzhus')
# 可以通过在模板中如下使用
{% for gz in u.guanzhus.all %}
<span>{{gz.guanzhu.name}}</span>
{% endfor %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment