I'm trying to add extra field name base on its sub domains, but i cant do that, here is my views.py
def daily_vistors_detail_html(request):
address = request.build_absolute_uri('/')
host_name = address.partition('://')[2]
sub_addr = host_name.partition('.')[0]
hotel_name = 'hotel 1'
if sub_addr == 'hotel 2':
hotel_name = 'hotel 2'
lists = MyModel.objects.annotate(hotel_name=hotel_name).order_by('-pk')
context = {
'lists':lists
}
but it returns
QuerySet.annotate() received non-expression(s): hotel 1. is there away to achieve that please?
thank you
CodePudding user response:
I think what you search is the annotation of a Value.
# Import Value:
from django.db.models import Value
def daily_vistors_detail_html(request):
address = request.build_absolute_uri('/')
host_name = address.partition('://')[2]
sub_addr = host_name.partition('.')[0]
hotel_name = 'hotel 1'
if sub_addr == 'hotel 2':
hotel_name = 'hotel 2'
# Annotate content of hotel_name as Value:
lists = MyModel.objects.annotate(hotel_name=Value(hotel_name)).order_by('-pk')
context = {
'lists':lists
}
You can find the docs here: Django Docs
