简体   繁体   English

Django一对多关系查询

[英]Django one to many Relationship Query

Well, I'm starting learning Django and now I have problems to get a QuerySet that allows me fill a table. 好吧,我开始学习Django,现在我在获取一个QuerySet来填充表格时遇到了问题。

The table shows information about the projects of a programer. 该表显示有关编程器项目的信息。 Each project has n iterations. 每个项目都有n次迭代。

|id|Name|Start|End|Cost|Objetive|Description|Client|Progress|Status| | id |名称|开始|结束|成本|对象|描述|客户|进度|状态| Iterations| 迭代|

In the View, I can get the first 9 columns (From the Project model) using a QuerySet like this: 在视图中,我可以使用QuerySet来获取前9列(从Project模型中获取),如下所示:

projects = Project.objects.filter(responsible = request.user.id)

In the 10th column I have to show the number of "Iterations" of every Project and I'm trying this: 在第十列中,我必须显示每个项目的“迭代次数”,而我正在尝试这样做:

Proyect.objects.filter(responsible = request.user.id).annotate(Niterations= Iteration.objectsfilter(project_id= request.user.id).count())

And well,it doesn't work, I understand that with an annotation the arguments will be added to each object in the QuerySet ... 嗯,这行不通,我了解使用注释将参数添加到QuerySet中的每个对象中。

The other way Iwas trying is: 我正在尝试的另一种方法是:

projects = Project.objects.filter(responsible = request.user.id)
for proy in projects:
   proy.annotate(Iteration.objects.filter(project_id=proy.pk).count())

I don't know exactly whay it doesn't works... Is there an easier way? 我不知道到底是怎么回事……有没有更简单的方法?

It's been a little confusing ... 有点令人困惑...

I'm working with two models: "Project" and "Iteration" 我正在使用两个模型:“项目”和“迭代”

Model: Project 型号:项目

from django.db import models
from usuarios.models import User

class Proyect(models.Model):
    name = models.CharField(max_length=255)
    start = models.DateField()
    end = models.DateField(blank=True, null=True)
    cost = models.FloatField(blank=True, null=True)
    objetive = models.TextField(blank=True, null=True)
    description = models.CharField(max_length=255, blank=True, null=True)
    client = models.CharField(max_length=255, blank=True,null=True)
    progress = models.FloatField(verbose_name=u'percentage of progress', default=0.00, editable=False)
    responsible = models.ForeignKey(User, editable=False)
    STATUS_CHOICES = (
        ('A','Atrasado'),
        ('N','Normal'),
        ('AP','Alta prioridad'),        
        )
    status = models.CharField(max_length=2, choices=STATUS_CHOICES)

   def __unicode__(self):
        return self.name

Model: Iteration 型号:迭代

from django.db import models
from proyectos.models import Project

class Iteration(models.Model):
    identifier = models.CharField(max_length=255)
    start = models.DateField()
    end = models.DateField()
    progress = models.FloatField(default=0.0, editable=False)
    project = models.ForeignKey(Project)

Thanks in advance!!! 提前致谢!!!

(I translated this, sorry if there are mistakes in translations) (我翻译了这个,如果翻译中有错误,抱歉)

The right way is: 正确的方法是:

Project.objects.filter(responsible=request.user.id).annotate(niterations=Count('iteration')

You can see the query to understand better: 您可以查看查询以更好地理解:

result = Project.objects.filter(responsible=request.user.id).annotate(niterations=Count('iteration')
print str(result.query)

I hope I've helped. 希望能对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM