Django increment views + 1
i dont know why this time i cant show the "number of views" of a items, i was doing this before but in this project i cant
def getAuto(request,marca,slug,id):
from django.db.models import F
object = get_object_or_404(Robado,marca__slug=marca,modelo__slug=slug,pk=id,publico=True)
object.views= F('views')+1
obje开发者_运维问答ct.save()
template.html
views: {{object.views}}
The template.html is showing:
(+: (DEFAULT: ), 1)
i cant imagine why
Thanks you guys
I don't think you can use F()
like this. It's for use in filter
expressions, where you can use it to compare one field with another field on the same model, and in update
expressions, where you can increment a field in place:
Robado.objects.filter(foo=bar).update(views=F('views')+1)
but I don't see why you're trying to use it here, where you already have the object. It's easier just to reference the attribute directly:
object.views = object.views + 1
or even more simply:
object.views += 1
精彩评论