How to pass string as parameter name?


username

I have the following function:

def create_act(user, verb, fk_name=None, fk_value=None):
    fk = getattr(Action, fk_name)
    action = Action(user=user, verb=verb, fk=fk_value)
    action.save()

Action is a class. The class has multiple properties and at first I don't know which property will get the value.

I make the property name dynamic.

I want the kwarg fkto be the actual property of the class action. FK can be an account or a company.

class Action(models.Model):

account = models.ForeignKey(Account, blank=True, null=True, related_name='activity', on_delete=models.CASCADE)
company = models.ForeignKey(Company, blank=True, null=True, related_name='activity', on_delete=models.CASCADE)

I found some answers on forums, but nothing related to me, or python. I've seen some suggestions on other sites to use eval, but eval is not safe.

schwobaseggl

Use dictand unpack together **. Note that - if fk_nameis the name of a ForeignKey- fkit won't be a string, but a ForwardManyToOneDescriptor. You may still want to set a property named fk_name:

action = Action(user=user, verb=verb, **{fk_name: fk_value})

Related


How to pass parameter name as parameter?

Olivier Pons I have the following code: hobbies2 = form.cleaned_data.pop('hobbies2') PersonneHobby.objects.filter(personne=obj).delete() for pk_str in hobbies2: try: hobby = TagTraduit.objects.get(pk=int(pk_str)) p = PersonneHobby.objects.c

How to pass a modified string parameter?

kaan_a I'm in chapter 12 of the Rust programming language, which implements a case-insensitive line search. It doesn't make sense to me to implement the same logic twice, so I figured out if I just call the case-sensitive search function with a case-sensitive

How to pass string in url parameter?

not any From API i get something like this: processId=22, now i want to pass it in url parameter but the problem is i need to pass key and value. How can i paste the whole string as parameter. Any suggestions? So what I want to achieve is: <a *ngIf="menu.refPa

How to pass parameter name as parameter to function

early pd I understand that the title probably doesn't make any sense. I searched everywhere but couldn't find an answer. What I'm trying to do is create a function that will take the parameter names of another function, a vector, and then proceed to call the f

How to convert string to parameter name and pass it to function?

Chiaki So I have this function def f(a=1, b=1): print("a:", a, "| b:", b) And this dictionary args = {"a": 4, "b": 5}. What I want to do is call a loop f(a=4)on the first iteration and the f(b=5)second . So basically something like this for key,value in a

How to pass parameter name as parameter?

Olivier Pons I have the following code: hobbies2 = form.cleaned_data.pop('hobbies2') PersonneHobby.objects.filter(personne=obj).delete() for pk_str in hobbies2: try: hobby = TagTraduit.objects.get(pk=int(pk_str)) p = PersonneHobby.objects.c

How to pass parameter name as parameter?

Olivier Pons I have the following code: hobbies2 = form.cleaned_data.pop('hobbies2') PersonneHobby.objects.filter(personne=obj).delete() for pk_str in hobbies2: try: hobby = TagTraduit.objects.get(pk=int(pk_str)) p = PersonneHobby.objects.c

How to pass parameter name as parameter?

Olivier Pons I have the following code: hobbies2 = form.cleaned_data.pop('hobbies2') PersonneHobby.objects.filter(personne=obj).delete() for pk_str in hobbies2: try: hobby = TagTraduit.objects.get(pk=int(pk_str)) p = PersonneHobby.objects.c

How to pass string as parameter name?

username I have the following function: def create_act(user, verb, fk_name=None, fk_value=None): fk = getattr(Action, fk_name) action = Action(user=user, verb=verb, fk=fk_value) action.save() Action is a class. The class has multiple properties an

How to pass a modified string parameter?

kaan_a I'm in chapter 12 of the Rust programming language, which implements a case-insensitive line search. It doesn't make sense to me to implement the same logic twice, so I figured out if I just call the case-sensitive search function with a case-sensitive

How to pass a modified string parameter?

kaan_a I'm in chapter 12 of the Rust programming language, which implements a case-insensitive line search. It doesn't make sense to me to implement the same logic twice, so I figured out if I just call the case-sensitive search function with a case-sensitive

How to pass a modified string parameter?

kaan_a I'm in chapter 12 of the Rust programming language, which implements a case-insensitive line search. It doesn't make sense to me to implement the same logic twice, so I figured out if I just call the case-sensitive search function with a case-sensitive

How to pass parameter name as parameter to function

early pd I understand that the title probably doesn't make any sense. I searched everywhere but couldn't find an answer. What I'm trying to do is create a function that will take the parameter names of another function, a vector, and then proceed to call the f

How to pass parameter name as parameter to function

early pd I understand that the title probably doesn't make any sense. I searched everywhere but couldn't find an answer. What I'm trying to do is create a function that will take the parameter names of another function, a vector, and then proceed to call the f

How to pass multiline String parameter?

username Suppose I have this method: def read_line_by_line(some_text) some_text.each |line| do (something) end end How can I do this? I have got: my first line of the input text I tried to pass it as a parameter but got a weird output. It doesn't read line

How to pass parameter name as parameter?

Olivier Pons I have the following code: hobbies2 = form.cleaned_data.pop('hobbies2') PersonneHobby.objects.filter(personne=obj).delete() for pk_str in hobbies2: try: hobby = TagTraduit.objects.get(pk=int(pk_str)) p = PersonneHobby.objects.c

How to pass parameter name as parameter?

Olivier Pons I have the following code: hobbies2 = form.cleaned_data.pop('hobbies2') PersonneHobby.objects.filter(personne=obj).delete() for pk_str in hobbies2: try: hobby = TagTraduit.objects.get(pk=int(pk_str)) p = PersonneHobby.objects.c

How to pass parameter name as parameter?

Olivier Pons I have the following code: hobbies2 = form.cleaned_data.pop('hobbies2') PersonneHobby.objects.filter(personne=obj).delete() for pk_str in hobbies2: try: hobby = TagTraduit.objects.get(pk=int(pk_str)) p = PersonneHobby.objects.c

How to pass parameter name as parameter?

Olivier Pons I have the following code: hobbies2 = form.cleaned_data.pop('hobbies2') PersonneHobby.objects.filter(personne=obj).delete() for pk_str in hobbies2: try: hobby = TagTraduit.objects.get(pk=int(pk_str)) p = PersonneHobby.objects.c

How to pass string as parameter name?

username I have the following function: def create_act(user, verb, fk_name=None, fk_value=None): fk = getattr(Action, fk_name) action = Action(user=user, verb=verb, fk=fk_value) action.save() Action is a class. The class has multiple properties an

How to convert string to parameter name and pass it to function?

Chiaki So I have this function def f(a=1, b=1): print("a:", a, "| b:", b) And this dictionary args = {"a": 4, "b": 5}. What I want to do is call a loop f(a=4)on the first iteration and the f(b=5)second . So basically something like this for key,value in a

How to pass parameter name as parameter to function

early pd I understand that the title probably doesn't make any sense. I searched everywhere but couldn't find an answer. What I'm trying to do is create a function that will take the parameter names of another function, a vector, and then proceed to call the f

How to pass parameter name as parameter to function

early pd I understand that the title probably doesn't make any sense. I searched everywhere but couldn't find an answer. What I'm trying to do is create a function that will take the parameter names of another function, a vector, and then proceed to call the f

How to pass parameter name as parameter to function

early pd I understand that the title probably doesn't make any sense. I searched everywhere but couldn't find an answer. What I'm trying to do is create a function that will take the parameter names of another function, a vector, and then proceed to call the f