我有一个书的模型:
class Book(models.Model):
publish_house = models.ForeignKey(PublishHouse, on_delete=models.DO_NOTHING)
author = models.ForeignKey(Author, on_delete=models.DO_NOTHING)
title = models.CharField(max_length=50)
price = models.DecimalField(max_digits=5, decimal_places=2)
objects = models.Manager()
img = models.ImageField(upload_to='books/', default='books/no.png')
img_inside = models.ImageField(upload_to='books/',
default='books/no_inside.png')
is_available = models.BooleanField(default=True)
book_date = models.DateTimeField(default=datetime.now, blank=True)
is_best_selling = models.BooleanField()
def __str__(self):
return self.title
class Meta:
db_table = 'Book'
我想按日期显示模板中的书籍(显示最近添加的书籍),这是 View :
def index(request):
new_books = Book.order_by('-book_date')[:4]
bestselling_books = Book.objects.filter('is_best_selling')[:4]
book_context = {
'new_books': new_books,
'bestselling_books': bestselling_books,
}
advertises = Advertise.objects.all()
adv_context = {
'advertises': advertises
}
return render(request, 'pages/index.html', adv_context, book_context)
我想按添加日期的顺序显示书籍
它一直报错:
type object 'Book' has no attribute 'order_by'
最佳答案
图书没有属性 order_by
,它的经理有
所以你需要
Book.objects.order_by
关于python - 类型对象 'Book' 没有属性 'order_by',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55007405/