使用Through表格查询递归的多对多关系,Django

时间:2017-02-17 13:39:35

标签: django django-models

我的models.py是:

User(models.Model):
  people = models.ManyToManyField('self', through='Friend', symmetrical=False)

Friend(models.Model):
  from_user = models.ForeignKey('User', related_name='friends')
  to_user = models.ForeignKey('User', related_name='friends_of')
  accepted = models.BooleanField(default=False)

要获取与User相关的所有Friend个对象的user_instance个实例的查询集,我目前正在执行以下操作:

user_instance.people.all() | user_instance.user_set.all()

这样可行,但我想要的仅是accepted = True。类似的东西:

user_instance.people.filter(people__accepted=True) | user_instance.user_set.filter(people__accepted=True)

注意:将友情重命名为人

由于

1 个答案:

答案 0 :(得分:1)

试试这个:

node_modules/rxjs/src

关键是,function wh_alter_attachment_image($attr) { if (isset($attr['srcset'])) unset($attr['srcset']); if (isset($attr['src'])) { $attr['data-original'] = $attr['src']; unset($attr['src']); } return $attr; } add_filter('wp_get_attachment_image_attributes', 'wh_alter_attachment_image'); 应该添加到user_instance.people.filter(friends_of__accepted=True) | user_instance.user_set.filter(friends__accepted=True) 而不是字段名称related_name