是否有一个选项可以防止某些值显示在 django 自动完成小部件中?

发布于 2024-11-30 14:00:33 字数 525 浏览 1 评论 0原文

我在“编辑用户详细信息视图”中使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红颜悴 2024-12-07 14:00:33

这不是一个完整的答案,但您应该按照这些思路做一些事情:

autocomplete = AutocompleteView()

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

    autocomplete.register(Profile.friends, UserAutocomplete)

但这不会排除当前用户。为此,您需要重写/扩展 ProfileAutocomplete 类的 view 方法。在此方法中,您需要获取用户 ID(可能来自会话),然后将其从查询集中排除。如果使用会话不起作用(有可能,我没有花太多时间),您可能需要修改 jquery_autocomplete.js 脚本以将用户传递给 查看方法。

It's not a complete answer, but you should do something along those lines:

autocomplete = AutocompleteView()

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

    autocomplete.register(Profile.friends, UserAutocomplete)

But this will not exclude the current user. To get that you will have override/extend the view method of your ProfileAutocomplete 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 the jquery_autocomplete.js script to pass the user to the view method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文