Op zondag 11-03-2007 om 17:39 uur [tijdzone +0100], schreef Horst
Gutmann:
> enquest schrieb:
> > Op zondag 11-03-2007 om 15:48 uur [tijdzone +0100], schreef Horst
> > Gutmann:
> >> enquest schrieb:
> >>> Second question: how can you add a password field from the newforms. you
> >>> can't define ("correct me if I'm wrong") a password field in the
> >>> model... And I don't see any option to give a field a password field.
> >>>
> >>> How can I do this?
> >>>
> >>> Thanxs
> >>>
> >> I haven't really tried it so this is probably just a wild guess, but the
> >> form_for_model function has a formfield callback function that will be
> >> executed on each field in your model and returns a new instance of the
> >> FormField to be used for the respective field.
> >>
> >> If you then instantiate your password field to use a CharField that uses
> >> the PasswordInput widget I think it should work :-) (But again: Wild
> >> guess ;-) )
> >>
> >> Best regards
> >>
> > 
> > Could you give an example so that I better can understand this.
> > 
> > Enquest
> > 
> 
> I mean something like this:
> 
> from django.db import models
> from django import newforms as forms
> from django.utils.text import capfirst
> 
> class PasswordField(models.CharField):
>       def formfield(self, **kwargs):
>               defaults = {'max_length': self.maxlength, 'required': not 
> self.blank,
> 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
>               defaults.update(kwargs)
>               defaults['widget']=forms.PasswordInput
>               return forms.CharField(**defaults)
>       def get_internal_type(self):
>               return "CharField"
> 
> # Create your models here.
> class Test(models.Model):
>       username=models.CharField(maxlength=255)
>       password=PasswordField(maxlength=255)
>       
> 
> >From I can tell it seems to work this way.
> 
> A More specific approach is described in [1] which changes the
> base_fields dict for a specific form class "at runtime"
> 
> I hope this helps you :-)
> 
> Best regards, Horst
> 
> [1] http://www.djangosnippets.org/snippets/55/
> 
> > 

Ok (thanxs) that I will try and figure out today.

However one question is still open!
How come the CHOICES lists arn't in the forms if you import you class?

Enquest


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to