access request.user in class Meta

2008-11-01 Thread Merrick

I am trying to dynamically generate a form, the fields will vary if
the user is logged in. Here is where I am so far

forms.py
==
class LinkForm(ModelForm):

def __init__(self, *args, **kw):
self.request = kw.pop('request')
super(LinkForm, self).__init__(*args, **kw)

class Meta:
model = Link
if self.request.user:
exclude = ['ip_address', 'timestamp', 'user', 'method', ]
else:
exclude = ['ip_address', 'timestamp', 'user', 'method',
'notes', 'private_stats',]

views.py
==
def index(request):
form = LinkForm(request=request)

The error I get makes it clear I don't have access to request within
class Meta:

NameError at /
name 'self' is not defined

Thanks,

Merrick

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: access request.user in class Meta

2008-11-03 Thread bruno desthuilliers

On 1 nov, 23:57, Merrick <[EMAIL PROTECTED]> wrote:
> I am trying to dynamically generate a form, the fields will vary if
> the user is logged in. Here is where I am so far
>
> forms.py
> ==
> class LinkForm(ModelForm):
>
> def __init__(self, *args, **kw):
> self.request = kw.pop('request')
> super(LinkForm, self).__init__(*args, **kw)
>
> class Meta:
> model = Link
> if self.request.user:
> exclude = ['ip_address', 'timestamp', 'user', 'method', ]
> else:
> exclude = ['ip_address', 'timestamp', 'user', 'method',
> 'notes', 'private_stats',]

This is a Python problem, not a Django problem. I strongly suggest you
take time to learn Python.


When the 'class Meta:' statement is eval'd, the LinkForm class doesn't
even yet exists - so there's no hope an instance of LinkForm could
exists.

FWIW, there's nothing special with name 'self' in Python. Instance
methods receive the current instance as first argument, and by
convention, this argument is named 'self', but you could name it
'ekky_ekky_ekky_ekky_zBang' just the same.

To make a long story short, what you're trying to do just cannot work
- or at least, not that way.


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---