0

我已经建立了一个 Django 项目,我在其中创建了随机点。这些随机点存储在数据库(sqlite)中(我可以通过管理网站查看它们并更改值,所以这是可行的)。如果我写一个脚本,我可以访问这些点并将它们打印在一个情节中。请参阅下面的代码。

但是,如果我想挖掘这些点以对它们进行排序或仅绘制数据集的选择,我似乎遇到了麻烦。如果我读出它们不再连接的值并且对 x 进行排序会混淆点集。有没有办法将数据集排序为在这种情况下为 X 的最小值并对值进行排序并打印集合?(保持点的所有 x、y、z 和名称值不变?)(见下面的答案,point in Point3D.objects.all().order_by('x'):

如果我现在想让 x 的值介于 x = 12 和 x = 30 之间?我怎样才能添加这个额外的过滤器?

我的代码如下:models.py:

class Point3D(models.Model):
name = models.CharField(max_length = 10)
x = models.DecimalField(max_digits=5, decimal_places=2)
y = models.DecimalField(max_digits=5, decimal_places=2)
z = models.DecimalField(max_digits=5, decimal_places=2)

生成点:

from books.models import Point3D

def points():
    for i in range(20):
        x = random.randint(0,100)
        y = random.randint(0,100)
        z = random.randint(0,100)

        p = Point3D(name = x , x = x ,y = y,z = z)
#        print 'test'
        p.save()
#    

points()

在views.py中:

def ThreeGraphs(request):
    fig = Figure()
    fig.suptitle('2D-punten')
    ax = fig.add_subplot(111)

    for point in Point3D.objects.all():
        print point
        name = int(point.name)
        xs = int(point.x)
        ys = int(point.y)
        zs = int(point.z)
        print (xs, ys, zs)
        ax.plot(xs, ys, 'bo' )

    HttpResponse(mimetype="image/png")
    FigureCanvas(fig)
    fig.savefig('template/images/testing.png')
    picture = "testing.png"
    return render_to_response('Test.html', {'picture': picture}, RequestContext(request))

希望有人知道如何解决我的麻烦。

非常感谢!蒂尔

4

1 回答 1

1

你需要这样做:

    for point in Point3D.objects.all().order_by('x'):

这将按“x”字段按排序顺序返回点。你可以说 order_by('-x') 来反转排序顺序。

于 2011-03-09T22:16:49.630 回答