也许我的方法/查询是完全错误的,但是无论如何,我正在尝试列出书籍和对它们进行的最新编辑。一本书可以被多次编辑,每个编辑都有一个类别。
Book(models.Model)
BookEdit(models.Model):
book=models.ForeignKey(Book,related_name='book_edits')
editedon=models.DateTimeField(auto_now_add=True)
action=models.CharField(max_length=250
现在在我看来,我只是在做:
books=Book.objects.value('id','book_edits__action').latest('book_edits__editedon')
try:
page=request.query_params.get('page',1)
paginator=Paginator(books,20)
data=paginator.page(page)
except PageNotAnInteger:
data=paginator.page(1)
except EmptyPage:
data=paginator.page(paginator.num_pages)
我已经注意到,它不带Latest()即可工作,后者似乎会输出字典对象。如何将最新()与分页器一起使用
答案 0 :(得分:1)
latest()
returns a single object,而不是查询集。因此,尝试对其进行分页没有任何意义。
您似乎想用最新的编辑日期注释结果。您需要使用an annotation来做到这一点。像这样:
from __future__ import print_function
import numpy as np
from keras.optimizers import SGD
np.random.seed(1337)
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers.core import Dense, Activation
from keras.layers.recurrent import LSTM
#from SpeechResearch import loadData
from sklearn.preprocessing import LabelEncoder
import pandas
'exception_verbosity = high'
batch_size = 5
hidden_units = 13
nb_classes = 10
print('Loading data...')
# load train dataset
dataframe = pandas.read_csv("train.csv", header=None)
dataset = dataframe.values
X_train = dataset[:,0:13] #.astype(float)
Y = dataset[:,13]
# encode class values as integers
encoder = LabelEncoder()
encoder.fit(Y)
encoded_Y = encoder.transform(Y)
# convert integers to dummy variables (i.e. one hot encoded)
y_train = np_utils.to_categorical(encoded_Y)
# load test dataset
dataframe = pandas.read_csv("test.csv", header=None)
dataset = dataframe.values
X_test = dataset[:,0:13] #.astype(float)
y_test = dataset[:,13]
# encode class values as integers
encoder2 = LabelEncoder()
encoder2.fit(y_test)
encoded_Y2 = encoder.transform(y_test)
# convert integers to dummy variables (i.e. one hot encoded)
Y_test = np_utils.to_categorical(encoded_Y2)
#(X_train, y_train), (X_test, y_test) = loadData.load_mfcc(10, 2)
print(len(X_train), 'train sequences')
print(len(X_test), 'test sequences')
print('X_train shape:', X_train.shape)
print('X_test shape:', X_test.shape)
print('y_train shape:', y_train.shape)
print('y_test shape:', y_test.shape)
print(y_test)
print('Build model...')
Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)
model = Sequential()
model.add(LSTM(output_dim=hidden_units, init='uniform', inner_init='uniform',
forget_bias_init='one', activation='tanh', inner_activation='sigmoid', input_shape=X_train.shape[1:]))
model.add(Dense(nb_classes))
model.add(Activation('softmax'))
sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer=sgd)
print("Train...")
model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=3, validation_data=(X_test, Y_test), show_accuracy=True)
score, acc = model.evaluate(X_test, Y_test,
batch_size=batch_size,
show_accuracy=True)
print('Test score:', score)
print('Test accuracy:', acc)
然后将向您的查询集中的每个对象添加一个from django.db.models import Max
Book.objects.annotate(last_edit=Max('book_edits__editedon'))
属性,对应于该对象的最新编辑。