消息撰写中的 Django 友谊

发布于 2024-09-29 16:28:02 字数 1295 浏览 3 评论 0原文

我正在使用 django-friends 和 django-messages。

我修改了我的自定义撰写表单以提取以下信息,我的朋友,并显示他们的全名而不仅仅是用户名。

我遇到的一个问题是,我似乎无法以登录用户的身份访问自己,要完成查询,我必须对其进行硬编码。

class MyComposeForm(forms.Form):
    """
    A simple default form for private messages.
    """
    recipient = forms.ModelChoiceField(queryset=Friendship.objects.all(), label=_(u"Recipient"))
    #recipient = forms.ModelChoiceField(queryset=User.objects.all(), label=_(u"Recipient"))
    subject = forms.CharField(label=_(u"Subject"))
    body = forms.CharField(label=_(u"Body"),
        widget=forms.Textarea(attrs={'rows': '2', 'cols':'55'}))

    def __init__(self, *args, **kwargs):
        recipient_filter = kwargs.pop('recipient_filter', None)
        super(MyComposeForm, self).__init__(*args, **kwargs)
        ### underneath here I have to hardcode with my ID to pull the info.
        friends = Friendship.objects.filter(from_user=1)
        self.fields['recipient'].choices = [(friend.to_user.pk, friend.to_user.get_full_name()) for friend in friends]
        if recipient_filter is not None:
            self.fields['recipient']._recipient_filter = recipient_filter

如何访问我的用户实例?

我尝试将 request 添加到 __init__ 并使用 request.user 但这似乎不起作用。

有什么想法吗?

I am using django-friends and django-messages.

I have modified my custom compose form to pull the following information, myfriends and also display their fullnames instead of just usernames.

One problem I have is that I cannot seem to access myself as a signed in user, to complete the query, I have to hard code it.

class MyComposeForm(forms.Form):
    """
    A simple default form for private messages.
    """
    recipient = forms.ModelChoiceField(queryset=Friendship.objects.all(), label=_(u"Recipient"))
    #recipient = forms.ModelChoiceField(queryset=User.objects.all(), label=_(u"Recipient"))
    subject = forms.CharField(label=_(u"Subject"))
    body = forms.CharField(label=_(u"Body"),
        widget=forms.Textarea(attrs={'rows': '2', 'cols':'55'}))

    def __init__(self, *args, **kwargs):
        recipient_filter = kwargs.pop('recipient_filter', None)
        super(MyComposeForm, self).__init__(*args, **kwargs)
        ### underneath here I have to hardcode with my ID to pull the info.
        friends = Friendship.objects.filter(from_user=1)
        self.fields['recipient'].choices = [(friend.to_user.pk, friend.to_user.get_full_name()) for friend in friends]
        if recipient_filter is not None:
            self.fields['recipient']._recipient_filter = recipient_filter

How do I access my user instance?

I have tried adding request to __init__ and using request.user but this does not seem to work.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

找回味觉 2024-10-06 16:28:02

您可以在表单中传递请求,例如:

form = MyComposeForm(request.POST,request) 

在已实例化表单的views.py 文件中。然后您可以访问请求对象:

requestObj = kwargs.pop('request', None)

您的代码将如下所示:

def __init__(self, *args, **kwargs):
    recipient_filter = kwargs.pop('recipient_filter', None)
    requestObj = kwargs.pop('request', None)
    super(MyComposeForm, self).__init__(*args, **kwargs)

You can pass request in your form like:

form = MyComposeForm(request.POST,request) 

in your views.py file, where the form has been instantiated. You can then access the request object as:

requestObj = kwargs.pop('request', None)

your code will look like:

def __init__(self, *args, **kwargs):
    recipient_filter = kwargs.pop('recipient_filter', None)
    requestObj = kwargs.pop('request', None)
    super(MyComposeForm, self).__init__(*args, **kwargs)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文