Django ManyToMany使用特定键进行反向查询

时间:2015-06-01 04:39:09

标签: django postgresql manytomanyfield

我有两张桌子。第一个是基本用户表。第二个是名为Section的表。

is_teacher=False

我想获得所有学生用户(在用户表中使用User.objects.filter(is_teacher=False)标识),我知道可以使用students = User.objects.filter(is_teacher=False) for s in students: print s.section_set.all() 轻松完成。

我想获取每个用户的活动部分。

但是,目前我甚至无法为用户获取部分的集合。

我试过了:

User

但是我收到section_set对象没有#include <iostream> #include <vector> #include <list> #include <string> using namespace std; class Vertex; class Edge{ public: Edge(Vertex *org, Vertex *dest, int weight){ origem = org; destino = dest; peso = weight; } Vertex* getOrigem(){return origem;} Vertex* getDestino(){return destino;} int getPeso(){return peso;} private: Vertex* origem; Vertex* destino; int peso; }; class Vertex{ public: Vertex(string id){ name = id; } void addEdge(Vertex *v, int peso){ Edge newEdge(this, v, peso); edges.push_back(newEdge); } void printEdges(){ cout << name << ":" << endl; for (int i = 0; i < edges.size(); i++) { Edge e = edges[i]; cout << e.getDestino()->getName() << " - " << e.getPeso() << endl; } cout << endl; } string getName(){return name;} vector<Edge> getEdges(){return edges;} private: string name; vector<Edge> edges; }; class Graph{ public: Graph(){ degree = 0; } void insert(Vertex* v){ vertices.push_back(v); degree++; } int getDegree(){return degree;} void printGraph(){ for (int i = 0; i < vertices.size(); i++) vertices[i]->printEdges(); } list<Vertex*> adj(Vertex* v){ vector<Edge> edges = v->getEdges(); cout << "oi\n"; list<Vertex*> adj; int nEdges = edges.size(); for(int i = 0; i < nEdges; i++) adj.push_back(edges.at(i).getDestino()); cout << "k\n"; } Vertex* getVertex(int i){return vertices.at(i);} private: vector<Vertex*> vertices; int degree; }; int main(){ list<list<Vertex*> > listAdj; Graph g; Vertex v1 = Vertex("Seattle"); Vertex v2 = Vertex("Portland"); Vertex v3 = Vertex("Everett"); Vertex v4 = Vertex("Lynnwood"); Vertex v5 = Vertex("Northgate"); Vertex v6 = Vertex("Bellevue"); Vertex v7 = Vertex("Arlington"); Vertex v8 = Vertex("Bellingham"); Vertex *vp1 = &v1; Vertex *vp2 = &v2; Vertex *vp3 = &v3; Vertex *vp4 = &v4; Vertex *vp5 = &v5; Vertex *vp6 = &v6; Vertex *vp7 = &v7; Vertex *vp8 = &v8; v1.addEdge(vp2, 100); v1.addEdge(vp6, 20); v2.addEdge(vp1, 100); v3.addEdge(vp1, 30); v3.addEdge(vp4, 10); v3.addEdge(vp7, 20); v4.addEdge(vp5, 15); v5.addEdge(vp1, 10); v6.addEdge(vp1, 20); v8.addEdge(vp7, 45); g.insert(vp1); g.insert(vp2); g.insert(vp3); g.insert(vp4); g.insert(vp5); g.insert(vp6); g.insert(vp7); g.insert(vp8); cout << "Cheguei aqui" << endl; for(int i = 0; i < g.getDegree(); i++) listAdj.push_back(g.adj(g.getVertex(i))); int k = 0; cout << "Cheguei aqui" << endl; for(list<Vertex*> i: listAdj){ cout << g.getVertex(k)->getName() << ":" << endl; list<Vertex*>::const_iterator j = i.begin(); for(; j != i.end(); j++){ Vertex *v = *j; cout << v->getName() << endl; } k++; } g.printGraph(); return 1; } 的错误。我猜测因为该部分与用户表(教师和学生)有两对多关系,我可能必须更清楚地指定关系(跟随学生而不是教师一)。但我不知道该怎么做。

3 个答案:

答案 0 :(得分:1)

在定义related_name值时,请注意这是向后关系的名称(在您的情况下 - 从UserSection)。因此,为了使您的代码清晰易懂,我建议您更改以下名称:

def Section(models.Model):
    ...
    teachers = models.ManyToManyField(User, related_name="sections_where_teacher")
    students = models.ManyToManyField(User, related_name="sections_where_student")
    is_active = models.BooleanField(default=True)
    ...

然后使用关系看起来像这样:

print s.sections_where_student.all()

答案 1 :(得分:0)

试试这个:

sec = Section.object.filter(students = students, is_active=True)

您将获得活跃学生的部分对象。

答案 2 :(得分:0)

您已在ManyToMany字段中明确定义related_name,因此您应该使用该访问者:

print s.students.all()

并不是说你使用的实际名字没有多大意义;反向关系不是&#34;学生&#34;,它&#34;部分作为学生&#34;。