3

I have Company and CompanyUser models. CompanyUser has 1:1 relationship with the django's auth User model. Every user is part of some company, so I have a ForeignKey field (Company) in the CompanyUser model:

class CompanyUser(models.Model):
    Company = models.ForeignKey(Company)
    User = models.OneToOneField(User)

Now I want to create some html tables to view and filter the data about Sales, Products, etc. I'm using django-tables2 and that works great.

Now let's say I want to see the Sales of a particular Product. So I need a dropdown that contains all Products of the Company that the user belongs to.

I've have created the forms.py file in my app:

from django import forms

class SaleFilterForm(forms.Form):
    product_id = forms.ChoiceField(queryset=Product.objects.all(Company=???))
    ...
    ...

So my question is. How to get the current user, when I'm inside forms.py file? I don't have the "request" object there..

user568021
  • 1,426
  • 5
  • 28
  • 55

2 Answers2

3

Views.py

def yourview(request):
    yourform = SaleFilterForm(request.POST, user=request.user)

Forms.py

class SaleFilterForm(forms.Form):
    ...

    def __init__(self, *args, **kwargs):
        self.user = kwargs.pop('user', None)
        super(SaleFilterForm, self).__init__(*args, **kwargs)

Then you can use your user inside of your form as self.user

Hope it helps!

Lara
  • 2,170
  • 6
  • 22
  • 43
  • I don't think I can.. I'm getting "name 'self' is not defined" – user568021 Jul 23 '15 at 13:10
  • Hi, check this out, http://stackoverflow.com/questions/1202839/get-request-data-in-django-form .. maybe you are missing something :) – Lara Jul 23 '15 at 13:13
  • Damn, this is quite challenging. The problem is this line: "product_id=forms.ChoiceField(queryset=Product.objects.all(Company=???))" ... I'm not inside the method so yeah.. there's no self.. I'm stuck.. – user568021 Jul 23 '15 at 13:18
  • You can define your fields or use your queryset inside of your init... There is no problem at all..example: http://stackoverflow.com/a/3533325/3350765 – Lara Jul 23 '15 at 13:41
  • sorry for bothering you, but I can't make this to work.. see my form here: http://pastebin.com/DaKCvAM1 it outputs nothing :( – user568021 Jul 24 '15 at 11:54
  • I think it's because you are using the super, you have also to define wich field will be in your form out of init, like the example that I sent. – Lara Jul 24 '15 at 11:59
  • With that example I'm getting: 'SaleFilterForm' object has no attribute 'fields' – user568021 Jul 24 '15 at 12:02
  • or if I use "self.product_id.queryset" i get: 'SaleFilterForm' object has no attribute 'product_id'... but it totally does.. you can see in my form – user568021 Jul 24 '15 at 12:03
  • I created a chat room, go there – Lara Jul 24 '15 at 12:09
-1

This is the best simple code in Django to pass user information form views.py to various files is by using a function

in the file, you want to access the current user info (ex:models.py) : after all imports

`

user = None
def current_user(request):
    global user
    user = request.user

`

and use the object variable user in the file

in views.py:

from .models import current_user

in any function(eg-def login(request):)

current_user(request)

It is just based on simple sending variable via the function. Simple logic