是否有一个选项可以防止某些值显示在 django 自动完成小部件中?
我在“编辑用户详细信息视图”中使用 django-autocomplete 来添加“朋友”
django-autocomplete 工作完美,但还显示“当前用户”(正在编辑他的个人资料)和用户“匿名”
我想排除这些二。
我怎样才能做到这一点?
模型.py
class Profile(UserenaLanguageBaseProfile):
friends = models.ManyToManyField(User,related_name='userfriends', blank=True, null=True)
形式.py
class EditProfileForm(forms.ModelForm):
class Meta:
widgets = {
'friends': MultipleAutocompleteWidget(Profile.friends),
}
I am using django-autocomplete in my "edit user details view" for adding "friends"
django-autocomplete works perfect but displays also the "current user" (who is editing his profile) and the user "anonymous"
I want to exclude those two.
How can I accomplish that?
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),
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是一个完整的答案,但您应该按照这些思路做一些事情:
但这不会排除当前用户。为此,您需要重写/扩展
ProfileAutocomplete
类的view
方法。在此方法中,您需要获取用户 ID(可能来自会话),然后将其从查询集中排除。如果使用会话不起作用(有可能,我没有花太多时间),您可能需要修改jquery_autocomplete.js
脚本以将用户传递给查看
方法。It's not a complete answer, but you should do something along those lines:
But this will not exclude the current user. To get that you will have override/extend the
view
method of yourProfileAutocomplete
class. In this method you will need to get the user ID (probably from the session) and then exclude it from the queryset. If using the session is not working (it's possible, I didn't put too much time on it), you 'll probably have to modify thejquery_autocomplete.js
script to pass the user to theview
method.