#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 HeatingUserForm ( forms.ModelForm):
def __init__(self, *args, **kwargs):
self.user=kwargs.pop('heating_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)
#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' ]
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> 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})
>
No comments:
Post a Comment