Accessing request object

41 views
Skip to first unread message

Salvatore DI DIO

unread,
Sep 21, 2014, 7:38:23 AM9/21/14
to django...@googlegroups.com
Hello,

Is it possible to access request object in 'helpers.py' file ?
Ihave tried 'crequest'  (from crequest.middleware import CrequestMiddleware)
without luck.


class AdminField(object):
    def __init__(self, form, field, is_first):
        self.field = form[field]  # A django.forms.BoundField instance
        self.is_first = is_first  # Whether this field is first on the line
        self.is_checkbox = isinstance(self.field.field.widget, forms.CheckboxInput)
        if (field == 'albums'):
            self.restrictQuery()

    def restrictQuery(self):
        from portfolio.models import Album
        #qs =  Album.objects.filter(title='Famille')
        qs =  Album.objects.all()
        self.field.field.queryset = qs


I would like to access 'request.user' in 'restrictQuery

Regards


Collin Anderson

unread,
Sep 22, 2014, 11:46:41 AM9/22/14
to django...@googlegroups.com
Can you attach the request to `form.request`?

Salvatore DI DIO

unread,
Sep 22, 2014, 4:16:50 PM9/22/14
to django...@googlegroups.com
Excuse me Collin, I don't understang what you are meaning ...

alTus

unread,
Sep 22, 2014, 7:30:18 PM9/22/14
to django...@googlegroups.com
Collin said you can attach your `request` object to the form object somewhere in your view. After that you will be able to use in anywhere in methods.
Also you can consider adding request parameter explicitly to your __init__ method and then passing it to `restrictQuery`.

PS. restrictQuery should be named restrict_query according to python style guide.

воскресенье, 21 сентября 2014 г., 15:38:23 UTC+4 пользователь Salvatore DI DIO написал:

Salvatore DI DIO

unread,
Sep 23, 2014, 1:52:22 AM9/23/14
to django...@googlegroups.com
I understand now,

Thank you very much Altus and Collin


--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/B4m9ki4hqp8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/033933b4-fb7b-46ca-bdf5-ada2011cb902%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages