码迷,mamicode.com
首页 > 其他好文 > 详细

related_name和related_query_name举例区别

时间:2018-12-06 23:58:51      阅读:285      评论:0      收藏:0      [点我收藏+]

标签:har   举例   xxx   users   type   pre   query   length   title   

例1:

class UserInfo(models.Model):
    nickname = models.CharField(max_length=32)
    username = models.CharField(max_length=32)
    password = models.CharField(max_length=64)

    gender_choices = (
        (1,‘男‘),
        (2,‘女‘),
    )
    gender = models.IntegerField(choices=gender_choices)
ralated_query_name 
class U2U(models.Model):
    g = models.ForeignKey(‘UserInfo‘,ralated_query_name=‘a‘)
    b = models.ForeignKey(‘UserInfo‘,ralated_query_name=‘b‘)

   # ralated_query_name

  #  obj.对象男.b_set.all()
  #  obj.对象女.a_set.all()
ralated_name
class U2U(models.Model):
    g = models.ForeignKey(‘UserInfo‘,ralated_name=‘a‘)
    b = models.ForeignKey(‘UserInfo‘,ralated_name=‘b‘)

    # ralated_name

    #  obj.对象男.b.all()
    #  obj.对象女.a.all()

例2:

反向查找

class UserType(models.Model):
    title = models.CharField(max_length=32)

class User(models.Model):
    username = models.CharField(max_length=32)
    ut = models.ForeignKey(‘UserType‘,related_name=‘xxx‘)

## related_name=‘xxx‘
反向:小写表名user_set ===>xxx
## related_query_name = ‘xxx‘
反向:user_set==>xxx_set

推荐使用

ut = models.ForeignKey(‘UserType‘,related_name=‘users‘)

 

related_name和related_query_name举例区别

标签:har   举例   xxx   users   type   pre   query   length   title   

原文地址:https://www.cnblogs.com/yuanyongqiang/p/10080392.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!