Thursday, May 30, 2013

Re: django 1.5 abstractuser / accessing information of logged on user

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> 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