开发者

Django friendship in messages compose

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?


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜