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 attributes, and I don't know at the beginning, which attribute will get a value.
I get the attribute name dynamic.
I want the kwarg fk
, to be an actual attribute of the class action. FK can be account or 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 on the forums some answers but nothing relevant to me, or in python. I saw some suggestion on other sites to use eval, but eval is not safe.