submit form as superuser vs regular user

33 views
Skip to first unread message

Danae Vogiatzi

unread,
Sep 18, 2017, 7:09:36 AM9/18/17
to Django users

In my django app I have a Myuser(User) class. It inherits the User class. When a new user is created the Myuser table is poplulated.

myusers.py

class Myuser(User):
    address = models.CharField(max_length=40)
    pobox = models.CharField(max_length=40)

models.py

class Someclass(models.Model):
    objectid    = models.IntegerField()
    objecttype  =  models.CharField(max_length=200)
    created     =  models.DateTimeField(default=timezone.now)
    modified    =  models.DateTimeField(auto_now=True)

class Someotherclass(Someclass):
    status = models.IntegerField(default=0,)
    name   =  models.CharField(max_length=200)
  created =  models.DateTimeField(default=timezone.now)
    modified =  models.DateTimeField(auto_now=True)
  user = models.ForeignKey(User)

forms.py

class SomeotherclassForm(forms.ModelForm):

    def __init__(self, *args, **kwargs):
        self.request = kwargs.pop("request")
        self.user = kwargs.pop('user')
        self.app = kwargs.pop('app')
        self.table = kwargs.pop('table')
        self.mytype = kwargs.pop('mytype')
        initial = kwargs.get('initial', {})
        super(SomeotherclassForm, self).__init__(*args, **kwargs)

create.py

class  DataCreate(CreateView):
  @method_decorator(login_required)
  def dispatch(self, *args, **kwargs):
     #some code here not relevant at all

  def get_form_kwargs(self):
        kwargs = super(DataCreate, self).get_form_kwargs()
        objectid = self.request.GET.get('objectid',None)
        objecttype = self.request.GET.get('objecttype',None)
        kwargs.update({'mytype': objecttype})
        kwargs.update({'request': self.request})
        kwargs.update({'user': self.request.user})
        kwargs.update({'app': self.app})
        kwargs.update({'table': self.kwargs['table'].lower()})
        return kwargs

  def form_valid(self, form):
        obj = form.save(commit=False)
        group = ''
        if not self.request.user.is_superuser:
            group = MyUser.objects.get(user_ptr_id=self.request.user.pk)
        else:
            groups = self.request.user.groups.all()    
        if  self.kwargs['table'] == 'Myprotocol':
                obj = form.save(commit=False)
                table = eval(self.request.GET.get('objecttype',None).title()).objects.get(pk=int(self.request.GET.get('objectid',None)))

                obj.objectid = table.pk
                obj.objecttype = table.__class__.__name__.lower()
                obj.user_id = self.request.user.pk
                obj.save()            
        else:
            obj = form.save()
        if self.request.POST.get('is_popup'):
            check = int(self.kwargs['is_list'])
            if self.kwargs['table'] == 'Someclass':
                popup = 1
                a = checkPopup2(obj,check,popup,obj.pk)
            else:
                a = checkPopup(obj,check)

            return a
        else:
            return super(DataCreate, self).form_valid(form)


When I have logged in as a regular user ,everything works fine. When I log in as a superuser, I get form error that objecttype,objectid and user are not filled. In my attempts to troubleshoot it , I realized that when I am logged in as a superuser ,it dowsn't reach the form_valid() function. I can't figure out why that is happening. Any suggestions or advice on how to troubleshoot it?

Avraham Serour

unread,
Sep 19, 2017, 9:30:35 AM9/19/17
to django-users
not sure if related, but the docs suggest to inherit from AbstractUser, not User



--
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...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/570196c4-7fd8-45ec-a33f-0713f0639228%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Melvyn Sopacua

unread,
Sep 21, 2017, 4:00:17 AM9/21/17
to django...@googlegroups.com
It doesn't reach form_valid because the form is not valid. It reaches
`form_invalid`. You can inspect things there for debugging. But the
error is probably with your authentication backed, since request.user
is not filled in. The obvious being that your superuser exists in
`auth_user` and other users in `yourapp_myuser`.
>> email to django-users...@googlegroups.com.
>> To post to this group, send email to django...@googlegroups.com.
>> Visit this group at https://groups.google.com/group/django-users.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/570196c4-7fd8-45ec-a33f-0713f0639228%40googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> 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...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAFWa6tKycjrHL5bOvE2H1wCvsXymO4zCDqCnAJknNOEKcyQV-Q%40mail.gmail.com.
>
> For more options, visit https://groups.google.com/d/optout.



--
Melvyn Sopacua
Reply all
Reply to author
Forward
0 new messages