2011-08-19 45 views
0

我使用Django的自動完成我的「修改用戶詳細信息查看」添加「好友」是否有一個選項可以防止在django自動填充小部件中顯示某些值?

Django的自動完成的作品完美,但顯示器還「當前用戶」(誰在編輯自己的配置文件)和用戶「匿名「

我想排除這兩個。

我該如何做到這一點?


models.py

class Profile(UserenaLanguageBaseProfile): 
    friends = models.ManyToManyField(User,related_name='userfriends', blank=True, null=True) 

forms.py

class EditProfileForm(forms.ModelForm): 
class Meta: 
     widgets = { 
      'friends': MultipleAutocompleteWidget(Profile.friends), 
     } 

回答

0

這不是一個完整的答案,但你應該做類似的規定:

autocomplete = AutocompleteView() 

class ProfileAutocomplete(AutocompleteSettings): 
    queryset = Profile.objects.exclude(friends='anonymous') 

    autocomplete.register(Profile.friends, UserAutocomplete) 

但這不會排除當前用戶。爲了得到那個,你將會覆蓋/擴展你的ProfileAutocomplete類的view方法。在這種方法中,您將需要獲取用戶標識(可能來自會話),然後將其從查詢集中排除。如果使用會話不工作(可能,我沒有花太多時間),您可能需要修改jquery_autocomplete.js腳本以將用戶傳遞給view方法。

相關問題