Thanks for the post Tom, I notice that the FormMixin class is from django.views.generic.edit and I am confused to how to use it so I gave it a try with #views.py class PremisesUpdateView(LoginRequiredMixin, FormMixin): form_class = PremisesForm model = Premises success_url = reverse_lazy('premlv') def get_form_kwargs(self): kwargs = super(PremisesUpdateView, self).get_form_kwargs() kwargs['heating_user'] = self.request.user return kwargs #forms.py class PremisesUpdateView(LoginRequiredMixin, FormMixin): form_class = PremisesForm model = Premises success_url = reverse_lazy('premlv') def get_form_kwargs(self): kwargs = super(PremisesUpdateView, self).get_form_kwargs() kwargs['heating_user'] = self.request.user return kwargs
however a problem now occurs with my urls.py , because not i get an error 'type object 'PremisesCreateView' has no attribute 'as_view' #urls.py ), url( regex=r'premuv/(?P<pk>\d+)/$', view=PremisesUpdateView.as_view(), name="premuv" ), do you know what I would substitute as_view() for , thanks for the grounding in these CBV, I am using them ala 2 scoops of django! On Thursday, 30 May 2013 11:14:27 UTC, Tom Evans wrote: > > On Thu, May 30, 2013 at 9:57 AM, tony gair <tonyt...@gmail.com<javascript:>> > wrote: > > > > I am using CBV / django braces for authentication. My user authorisation > > model inherits abstractuser and adds a few fields which are foreign keys > to > > two other models organisation and premises. > > > > When a form initialises I want to be able to examine the details of the > user > > logged in , look to see if they are a is_superuser or is_staff and > occupy > > my organisation and premises form fields accordingly using the values > stored > > as foreign keys in the logged on and authenticated user. > > > > My main problem at the moment is that I get a KeyError for the line > > 'self.user=kwargs.pop('user')' centered on 'user' > > > > > > #heating/forms.py > > from django import forms > > from .models import Organisation, Premises, Heating_User, Room, Heater > > > > 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() > > elif self.user.is_staff: > > self.fields['premises'].queryset = > > Premises.objects.filter(organisation=self.user.organisation) > > else: > > self.fields['premises'].queryset = > > Premises.objects.filter(id=self.user.premises) > > > > > > #models.py > > from django.conf import settings > > from django.core.urlresolvers import reverse > > from django.db import models > > from django.contrib.auth.models import AbstractUser > > > > class Heating_User (AbstractUser): > > jobtitle = models.CharField(max_length=255) > > organisation = models.ForeignKey(Organisation, null=True) > > premises = models.ForeignKey(Premises, null= True) > > def __unicode__(self): > > return self.username > > def get_absolute_url(self): > > return reverse('users-detail', kwargs={'pk':self.pk}) > > #end of files > > > > > > can anyone see where I am going wrong here? > > > > 'user' is not an argument that is supplied to every form that is > initialised, I was trying to explain this to you yesterday. > > Yesterday you said are using a CBV. A CBV that deals with forms > probably uses the FormMixin to provide it's form handling: > > > https://docs.djangoproject.com/en/1.5/ref/class-based-views/mixins-editing/#django.views.generic.edit.FormMixin > > > FormMixin defines various functions and attributes on the class, which > you can customise in your derived version of the class to give > specialised effects. The one you'll need to change in order to pass > user to the form constructor is get_form_kwargs(). You will want to do > something like this: > > class MyClassBasedView(UpdateView): > form_class = HeatingUserForm > def get_form_kwargs(self): > kwargs = super(MyClassBasedView, self).get_form_kwargs() > kwargs['user'] = self.request.user > return kwargs > > Then, when your class based view instantiates your form, it will > provide the correct arguments. > > 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+unsubscr...@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.