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