Re: Custom clean methods for admin site?

2008-11-07 Thread krase



On 3 Nov., 16:34, Bülent Aldemir <[EMAIL PROTECTED]> wrote:
> Create methods with  like:
>
> class MyModel(models.Model):
>     mystring = models.CharField(max_length=100)
>
> class MyForm(forms.ModelForm):
>    def clean_mystring(self):
>        raise forms.ValidationError('Error Message')
>

Thanks a lot! It works.
It even works with __getattr__, so i do not have to create a subclass
for
every model.

class MyForm(forms.ModelForm):
   def do_authorisation(self, name):
  print self.Meta.model._meta.verbose_name
  #raise forms.ValidationError('Error Message')
  return self.cleaned_data[name]

   def __getattr__(self, name):
  print name
  if name[:6] == 'clean_':
 return lambda: self.do_authorisation(name[6:])


Greets,
Sebastian

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



Custom clean methods for admin site?

2008-11-03 Thread krase

Hi,

i am trying to build an authorisation system for the admin site.
The authorisation should not only be based on user, group and type of
the object, but also
on contents the user just typed in.
I assume that i have to override the clean() methods of the ModelAdmin
classes.

First i tried this:

class MyForm(forms.ModelForm):
   def clean(self):
  #do authorisation here
  return super(MyForm, self).clean()

class FooAdmin(admin.ModelAdmin):
   form = MyForm
   model = Foo

The Problem here is that i can not raise ValidationErrors for single
fields.

Then i found this: 
http://code.djangoproject.com/wiki/NewformsHOWTO#Q:HowdoIaddcustomvalidation
The second example of this question seems to be the right one, but i
don't understand how to
create a custom clean() method here.

A quick an dirty example is very appreciated ;)


Greets,
Sebastian

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