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