How can I have keyword argument from string parameter?

notalentgeek

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]
AChampion

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I use `class` as a keyword argument?

From Dev

How can I write a function have a polymorphic return type based on the type argument of its type parameter?

From Dev

How can I pass argument with parameter to gulp?

From Dev

How can I access the default value for a keyword argument in Python?

From Dev

Can I define a Typescript function parameter to have a type of boolean or string?

From Dev

How can I define a &key argument that supersedes an &optional parameter in Lisp

From Dev

How can I pass multiline String parameter?

From Dev

How can I use a Resource as a string parameter?

From Dev

How can I split a string at a keyword in zshell and save the result?

From Dev

How can I avoid adding href to an overlapping keyword in string?

From Dev

Kialo: How can I view the argument topology map after I have entered an argument

From Dev

How can I only show the exact lines from a file with a keyword?

From Dev

How can I have an unused type parameter in a struct?

From Dev

How can I have an unused type parameter in a struct?

From Dev

How can I strip HTML tags that have attribute(s) from string?

From Dev

How can I strip HTML tags that have attribute(s) from string?

From Dev

Java: How can I inherit a constructor from a superclass with "java.lang.String[] args" as a parameter?

From Dev

How would I take a string from an argument and use it in an if statement?

From Dev

How can I specify an argument for the Repository's constructor from the Controller?

From Dev

How can I transition from a runtime value to a template argument?

From Dev

How can I get a specific argument from a previous command in bash?

From Dev

How can I conditionally pass an argument from a POSIX shell script?

From Dev

How can I get data argument from serializer?

From Dev

How can I pass an argument to a function when I have called it in the href tag

From Dev

How can I split string that have "[*word*]" as its part

From Dev

How can I have a String attached to an array of ints?

From Dev

How can I truncate a string to have at most N characters?

From Dev

How can I have toString() return a multi-line string?

From Dev

How can I remove a double string to have it only be the first word?

Related Related

  1. 1

    How can I use `class` as a keyword argument?

  2. 2

    How can I write a function have a polymorphic return type based on the type argument of its type parameter?

  3. 3

    How can I pass argument with parameter to gulp?

  4. 4

    How can I access the default value for a keyword argument in Python?

  5. 5

    Can I define a Typescript function parameter to have a type of boolean or string?

  6. 6

    How can I define a &key argument that supersedes an &optional parameter in Lisp

  7. 7

    How can I pass multiline String parameter?

  8. 8

    How can I use a Resource as a string parameter?

  9. 9

    How can I split a string at a keyword in zshell and save the result?

  10. 10

    How can I avoid adding href to an overlapping keyword in string?

  11. 11

    Kialo: How can I view the argument topology map after I have entered an argument

  12. 12

    How can I only show the exact lines from a file with a keyword?

  13. 13

    How can I have an unused type parameter in a struct?

  14. 14

    How can I have an unused type parameter in a struct?

  15. 15

    How can I strip HTML tags that have attribute(s) from string?

  16. 16

    How can I strip HTML tags that have attribute(s) from string?

  17. 17

    Java: How can I inherit a constructor from a superclass with "java.lang.String[] args" as a parameter?

  18. 18

    How would I take a string from an argument and use it in an if statement?

  19. 19

    How can I specify an argument for the Repository's constructor from the Controller?

  20. 20

    How can I transition from a runtime value to a template argument?

  21. 21

    How can I get a specific argument from a previous command in bash?

  22. 22

    How can I conditionally pass an argument from a POSIX shell script?

  23. 23

    How can I get data argument from serializer?

  24. 24

    How can I pass an argument to a function when I have called it in the href tag

  25. 25

    How can I split string that have "[*word*]" as its part

  26. 26

    How can I have a String attached to an array of ints?

  27. 27

    How can I truncate a string to have at most N characters?

  28. 28

    How can I have toString() return a multi-line string?

  29. 29

    How can I remove a double string to have it only be the first word?

HotTag

Archive