I want the field_name
to be inputted from string. Is this a possibility in Python?
earliest = Model.objects.filter(
field_name__year=latest_datetime.year,
field_name__month=latest_datetime.month,
field_name__day=latest_datetime.day
).order_by(field_name)[0]
I hope this function will give you a little but explanation on what I want.
def cool_function(field_name_from_param):
earliest = Model.objects.filter(
field_name_from_param__year=latest_datetime.year,
field_name_from_param__month=latest_datetime.month,
field_name_from_param__day=latest_datetime.day
).order_by(field_name_from_param)[0]
You could construct a dict
and use **
for argument unpacking, e.g.:
def cool_function(field_name_from_param):
earliest = Model.objects.filter(
**{field_name_from_param+"__year": latest_datetime.year,
field_name_from_param+"__month": latest_datetime.month,
field_name_from_param+"__day": latest_datetime.day}
).order_by(field_name_from_param)[0]
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments