2015-12-28 78 views
0

這裏是我的代碼:有沒有辦法傳遞命名參數?

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) 

所以我發現我可以很容易地通過一個類作爲參數。但最後一個問題是關於命名的論點。如果你看我的代碼,我會做一個langue=TagWithValue..[blabla]。問題是,這是一個「命名」參數,我希望能夠通過它這樣:

update_tags_with_value(self.cleaned_data.get('known_languages'), 
         PersonneLangue, 'langue') 
update_tags_with_value(self.cleaned_data.get('types_permis'), 
         PersonneTypesPermis, 'permis') 

然後以某種方式調用它像(它不工作尚未) :

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() 

現在我得到這個錯誤:

'champ' is an invalid keyword argument for this function 

更確切地說,我需要調用many_to_many_class.objects.create()一個時間known_languages=blabla和ANOT她的時間types_permis=blabla其中,換句話說,應該叫一次many_to_many_class.objects.create(known_languages=blabla)many_to_many_class.objects.create(types_permis=blabla),我想知道是否有一種方法來精確只有參數的名稱,blabla

如何解決這個問題?

+1

我不100%瞭解你想要什麼,但[命名參數解包](http://stackoverflow.com/questions/36901/what-does-double-star-and-star-do-for-python-parameters)可能對你有用。你可以創建一個字典並將它傳遞給一個函數,就好像它是一個關鍵字參數的集合。 – Kevin

回答

0

這是我工作的解決方案,我不知道這是否是最好的「Python化」的方式,但它就像一個魅力:

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: 
      args = { 
       'personne': self.instance, 
       champ: TagWithValue.objects.get(
        pk=idx_tag_with_value 
       )} 
      pl = many_to_many_class.objects.create(**args) 
      pl.save() 

update_tags_with_value(self.cleaned_data.get('known_languages'), 
         PersonneLangue, 'langue') 
update_tags_with_value(self.cleaned_data.get('types_permis'), 
         PersonneTypePermis, 'type_permis') 
update_tags_with_value(self.cleaned_data.get('diplomes'), 
         PersonneDiplome, 'diplome') 
update_tags_with_value(self.cleaned_data.get('centres_dinteret'), 
         PersonneCentreDInteret, 'centre_dinteret') 
update_tags_with_value(self.cleaned_data.get('hobbies'), 
         PersonneHobby, 'hobby') 
0

這是非常不推薦的,但它可以通過使用exec()函數。

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: 
      command = """pl = many_to_many_class.objects.create(
       personne=self.instance,""" 
       + 
       champ 
       + 
       """=TagWithValue.objects.get(
        pk=idx_tag_with_value 
       ) 
      )""" 
      exec(command) 
      pl.save() 

但正如我所說,這是不符合Python和很容易失敗。你應該對它進行真正的測試,以便繼續採用這種方法。

我的建議是將每個情況封裝到函數中,並根據傳遞的參數調用這些函數。
但是爲了回答你的問題,這是它完成的方式。

0

似乎正常的關鍵字解包會工作。 。 。

kwargs = {champ: TagWithValue.objects.get(
        pk=idx_tag_with_value)} 
pl = many_to_many_class.objects.create(
        personne=self.instance, 
        **kwargs) 

當然,現在champ可能不會對功能參數最好的名字,但希望你的想法。

+0

我的問題是,嚴格來說,我需要一次使用'known_languages = blabla'調用'many_to_many_class.objects.create()',另一次調用'types_permis = blabla',換句話說,應該調用一次'many_to_many_class .objects.create(known_languages = blabla)''''和'many_to_many_class.objects.create(types_permis = blabla)'我想知道是否有一種方法可以精確*只有參數的名稱*,** not **' blabla' –

相關問題