从Django查询中创建一个“矩阵”

[英]Make a “matrix” from Django query


I have a model similar to this one:

我有一个类似于这个的模型:

class MyModel(models.Model):
    name = models.CharField(max_length = 30)
    a    = models.ForeignKey(External)
    b    = models.ForeignKey(External, related_name='MyModels_a')

    def __unicode__(self):
        return self.a + self.b.name + self.b.name

So when I query it I get something like this:

所以当我查询它时,我得到这样的东西:

>>> MyModel.objects.all()
[<MyModel: Name1AB>,<MyModel: Name2AC>,<MyModel: Name3CB>,<MyModel: Name4BA>,<MyModel: Name5BA>]

And I'd like to represent this data similar to the following.

我想表示这些数据类似于以下内容。

[[ []                 , [Name1AB] , [Name2AC] ]
 [ [Name4BA, Name5BA] , []        , []        ]
 [ []                 , [Name3CB] , []        ]]

As you can see the rows would be 'a' in the model; and the columns would be 'b' I can do this, but it takes a long of time because in the real database I have a lot of data. I'd like to know if there's a Django built in way to do this.

正如您所看到的,模型中的行将是“a”;并且列将是'b'我可以做到这一点,但它需要很长时间,因为在真实数据库中我有很多数据。我想知道是否有一个Django内置的方法来做到这一点。

I'm doing it like this:

我是这样做的:

mymodel_list  = MyModel.objects.all()
external_list = External.objects.all()

for i in external_list:
    for j in external_list:

        print(mymodel_list.filter(a=i).filter(arrl=j).all(),end='')
        print()

Thanks

2 个解决方案

#1


1  

Three ways of doing it but you will have to research a bit more. The third option one may be the most suitable for what you are looking for.

这样做的三种方法,但你将不得不研究更多。第三种选择可能是最适合您所寻找的。

1) Django queries

1)Django查询

The reason it is taking a long time is because you are constantly accessing the database in this line:

它需要很长时间的原因是因为您不断访问此行中的数据库:

print(mymodel_list.filter(a=i).filter(arrl=j).all(),end='')

You may have to start reading what the Django documentation say about the way of working with queries. For what you are doing you have to create the algorithm to avoid the filters. Using MyModel.objects.order_by('a') may help you to build a efficient algorithm.

您可能必须开始阅读Django文档中有关使用查询的方式的内容。对于您正在做的事情,您必须创建算法以避免过滤器。使用MyModel.objects.order_by('a')可以帮助您构建有效的算法。

2) {% ifchanged ...%} tag

2){%ifchanged ...%}标签

I suppose you are using print to post your answer but you probably need it in html. In that case, you may want to read about the ifchanged tag. It will allow you to build your matrix in html with just one db access.

我想你使用print来发布你的答案,但你可能需要在html中。在这种情况下,您可能想要阅读ifchanged标签。它允许您使用一个db访问权限在html中构建矩阵。

3) Many to many relations

3)很多关系

It seems you are sort of simulating a many to many relation in a very peculiar way. Django has support for many to many relations. You will need an extra field, so you will also have to read this.

看起来你有点以非常特殊的方式模拟多对多的关系。 Django支持多对多的关系。您将需要一个额外的字段,因此您还必须阅读此内容。

Finally, for doing what you are trying with just one access to the database, you will need to read prefetch_related

最后,为了完成您只需一次访问数据库的操作,您需要读取prefetch_related

#2


0  

There's no built-in way (because what you need is not common). You should write it manually, but I'd recommend retrieving full dataset (or at least dataset for one table line) and process it in Python instead of hitting DB in each table cell.

没有内置方式(因为你需要的不常见)。您应该手动编写它,但我建议检索完整数据集(或至少一个表行的数据集)并在Python中处理它,而不是在每个表格单元格中命中DB。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2012/05/08/b5cfa6df079424f039aad9eda084305b.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号