Thursday, May 30, 2013

Re: linking the logged in user to other models to filter query sets -( noobie )

you just give the user object to the form object in you view handler.
some like:
    form = HeatingUserForm(user=request.user)
    ....
or if have request POST  data, you could do it like this:
    form = HeatingUserForm(request.POST, user=request.user)
then:
    self.user = kwargs.pop('user')
will be ok!


2013/5/30 tony gair <tonythegair@gmail.com>
with the line

        self.user = kwargs.pop('user')

do I need to import anything to access that the value 'user' ?
 
On Wednesday, 29 May 2013 15:16:32 UTC, Tom Evans wrote:
On Wed, May 29, 2013 at 3:29 PM, tony gair <tonyt...@gmail.com> wrote:
>
>
> I've done a basic django app with a user which contains an organisation and
> premises foreign key inside the user model which has been inherited using
> the features of django 1.5.
>
> I use the organisation id and premises id inside other models which I would
> like to present themselves providing they are the same.
>
> I have tried to closely follow the 'two scoops of django approach' in that I
> use a CBV with django braces approach to show my models.
>
> It is very important for me only to show the models in the views which use
> either the organisation id or premises id.
>
> At the moment in my  forms.py for my  user it looks like
>
> ****************************************************
> #forms.py
> class HeatingUserForm ( forms.ModelForm):
>
> premises = CustomModelChoiceField(queryset=Premises.objects.all())
> organisation = CustomModelChoiceField(queryset=Organisation.objects.all())
> class Meta:
> model=Heating_User
>         fields = [ 'username', 'password', 'first_name', 'last_name',
> 'email', 'is_staff', 'is_active', 'date_joined', 'jobtitle', 'organisation',
> 'premises' ]
>
> ***************************************************
>
> Unless the user is a super user, I want the organisation to be restricted to
> the organisation that the logged in user is of the same . The premises
> should be restricted to those premises that have the same id as the logged
> in user.
>
> Can anyone tell me how this is done?
>
> :)
>

You want to do different things in the form based upon the current
user, so the first step is to require that a user object is passed to
the form's __init__ method. Then, you simply manipulate the queryset
attributes on the desired fields after calling the super class'
__init__ method.

Also, if premises and organisation are foreign key fields, as it
seems, you don't need to declare the fields at all:

class HeatingUserForm (forms.ModelForm):
    def __init__(self, *args, **kwargs):
        self.user = kwargs.pop('user')
        super(HeatingUserForm, self).__init__(*args, **kwargs)
        if self.user.is_superuser:
            self.fields['premises'].queryset = Premises.objects.all()
            self.fields['organisation'].queryset = Organisation.objects.all()
        else:
            self.fields['premises'].queryset =
Premises.objects.filter(users=self.user)
            self.fields['organisation'].queryset =
Organisation.objects.filter(users=self.user)
    class Meta:
        model=Heating_User
        fields = [ … ]

You could also do some additional cleanup work, if the number of
choices for either of those two fields is exactly one, you could
replace the widget on the form field with a hidden input, so that you
do not display a pointless control (OTOH, it may not be pointless).

Cheers

Tom

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment