Here's my code:
def update_tags_with_value(tags, many_to_many_class):
if tags:
many_to_many_class.objects.filter(
personne=self.instance,
date_v_fin=None
).update(date_v_fin=django_datetime.now())
for idx_tag_with_value in tags:
pl = many_to_many_class.objects.create(
personne=self.instance,
langue=TagWithValue.objects.get(
pk=idx_tag_with_value
)
)
pl.save()
update_tags_with_value(self.cleaned_data.get('known_languages'),
PersonneLangue)
update_tags_with_value(self.cleaned_data.get('types_permis'),
PersonneTypesPermis)
So I found out I can easily pass a class as a parameter. But the last problem is about the named argument. If you watch my code, I do a langue=TagWithValue..[blabla]
. The problem is that it's a "named" parameter, and I'd like to be able to pass it like that:
update_tags_with_value(self.cleaned_data.get('known_languages'),
PersonneLangue, 'langue')
update_tags_with_value(self.cleaned_data.get('types_permis'),
PersonneTypesPermis, 'permis')
And then to call it somehow like that (it doesn't work yet):
def update_tags_with_value(tags, many_to_many_class, champ):
if tags:
many_to_many_class.objects.filter(
personne=self.instance,
date_v_fin=None
).update(date_v_fin=django_datetime.now())
for idx_tag_with_value in tags:
pl = many_to_many_class.objects.create(
personne=self.instance,
champ=TagWithValue.objects.get(
pk=idx_tag_with_value
)
)
pl.save()
For now I get this error:
'champ' is an invalid keyword argument for this function
To be more precise, I need to call many_to_many_class.objects.create()
one time with known_languages=blabla
and another time with types_permis=blabla
which, in other words, should call once many_to_many_class.objects.create(known_languages=blabla)
and many_to_many_class.objects.create(types_permis=blabla)
and I would like to know if there's a way to precise only the name of the parameter, not blabla
How to solve this?