0
我建立了一个Django项目,在该项目中我创建了随机点。这些随机点存储在数据库(sqlite)(我可以通过管理网站看到它们,并更改值,所以这个工程)。 如果我写了一个脚本,我可以访问这些点并将它们打印在一个图中。见下面的代码。与Django的数据挖掘问题
但是,如果我然后想挖掘这些点来排序它们或只绘制一个选择的数据集,我似乎有麻烦。如果我读出它们不再连接的值,排序x会混淆点集。 有没有办法将数据集排序到最小值,在这种情况下X和排序值并打印集? (保留点的所有x,y,z和名称值完好?)(请参阅下面的回答,point in Point3D.objects.all().order_by('x'):
)
如果我现在想要x = 12和x = 30之间的x值?我如何添加这个额外的过滤器?
我的代码如下: 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))
希望任何人知道如何解决我的麻烦。
非常感谢! Tijl
是否有可能在对列表进行排序后指定您只想选择说10:30?我知道Point3D.object.all()[10:30]可以工作,但是你还可以在排序后添加过滤器吗? – Tijl 2011-03-10 08:24:00
我的意思是我可以按x排序,然后只选择x = 12到x = 30的范围? – Tijl 2011-03-10 10:29:20
你想.filter(x__gte = 12,x__lte = 30)而不是.all() - 也http://docs.djangoproject.com/en/dev/topics/db/queries/ – 2011-03-10 18:03:28