I think my first post was a bit messy and I left some information out
I have an organisation as a foreign key contained in my abstact user object which is Heating_User. The premises also contains the organisation as a foreign key. What I am looking to do on my premises view is to filter the premises so that the organisation is the same as that for the logged in user. Although my model does include the premises id I want it so there is a organisation wide flag that would list all the premises if that flag was set. The superuser flag I (oh my that looks really useful). I think you have probably told me what I need to know but I'm not sure. I guess what I am asking in addition is how do I access the logged in user organisation foreign key so that I can filter the query? I hope that makes sense, I am quite new to this, but very enthused!!
On Wednesday, 29 May 2013 15:16:32 UTC, Tom Evans wrote:
-- 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.
No comments:
Post a Comment