目前我有这个型号:
class Group(models.Model):
def __str__(self):
return(self.name)
dependency = models.ManyToManyField('self',related_name='group_rel')
name = models.TextField()
class Publication(models.Model):
name = models.TextField()
group = models.ManyToManyField(Group, related_name='group_dependency')
我可以让所有父亲(团体)成为一个团体。
group = Group.objects.filter(pk=1)[0]
group.dependency.all()
但我想查询一个小组的所有儿童(群组)。
dir(group)
[&#39; DoesNotExist&#39;,&#39; MultipleObjectsReturned&#39;,&#39; class &#39;,&#39; delattr &#39;,&#39; dict &#39;,&#39; dir &#39;,&#39; doc &#39;,&#39; eq &#39;,&#39; 格式&#39;,&#39; ge < / strong>&#39;,&#39; getattribute &#39;,&#39; gt &#39;,&#39; 哈希&#39;,&#39; init &#39;,&#39; init_subclass &#39;,&#39; le &#39;,&#39; lt &#39;,&#39; 模块&#39;,&#39; ne &#39;,&#39; new &#39;,&#39; reduce &#39;,&#39; < strong> reduce_ex &#39;,&#39; repr &#39;,&#39; setattr &#39;,&#39; setstate &#39;,&#39; sizeof &#39;,&#39; str &#39;,&#39; ; 子类别&#39;,&#39; 弱点&#39;,&#39; _chec k_column_name_clashes&#39;,&#39; _check_field_name_clashes&#39;,&#39; _check_fields&#39;,&#39; _check_id_field&#39;,&#39; _check_index_together&#39;,&#39; _check_local_fields&#39;, &#39; _check_long_column_names&#39;,&#39; _check_m2m_through_same_relationship&#39;,&#39; _check_managers&#39;,&#39; _check_model&#39;,&#39; _check_model_name_db_lookup_clashes&#39;,&#39; _check_ordering& #39;,&#39; _check_swappable&#39;,&#39; _check_unique_together&#39;,&#39; _do_insert&#39;,&#39; _do_update&#39;,&#39; _get_FIELD_display&#39;,& #39; _get_next_or_previous_by_FIELD&#39;,&#39; _get_next_or_previous_in_order&#39;,&#39; _get_pk_val&#39;,&#39; _get_unique_checks&#39;,&#39; _meta&#39;,&#39; _perform_date_checks&# 39;,&#39; _perform_unique_checks&#39;,&#39; _save_parents&#39;,&#39; _save_table&#39;,&#39; _set_pk_val&#39;,&#39; _state&#39;,&# 39;检查&#39;,&#39;清除&#39;,&#39; clean_fields&#39;,&#39; date_error_message&#39;,&#39;删除&#39;,&#39;依赖&#39 ;,&#39; from_db&#39;,&#39; full_clean&#3 9;,&#39; get_deferred_fields&#39;,&#39; group_dependency&#39;,&#39; id&#39;,&#39; name&#39;,&#39; objects&#39;,&# 39; pk&#39;,&#39; prepare_database_save&#39;,&#39; refresh_from_db&#39;,&#39;保存&#39;,&#39; save_base&#39;,&#39; seri alizable_value&# 39;,&#39; unique_error_message&#39;,&#39; validate_unique&#39;]
注意:出版物是不使用此问题。我只是展示它,因为有很多人可以分组。
答案 0 :(得分:1)
您已将多对多字段命名为dependency
,因此您应该使用该字段。
group.dependency.all()