On Nov 26, 1:57 am, Daniel Roseman <[email protected]> wrote: > On Nov 25, 7:53 am, PRANAV HEGDE <[email protected]> wrote: > > > > > > > > > > > Hi guys, > > > I'm trying to create a login form using a model form, > > > user model: > > class users(models.Model): > > username = models.CharField(max_length=20, primary_key=True) > > password = models.CharField(max_length=50) > > usertype = models.CharField(max_length=20) > > class Meta: > > db_table = u'users' > > > login form: > > class loginlorm(forms.ModelForm): > > class Meta: > > model = users > > fields = ('username','password','usertype') > > widgets = { > > 'username':TextInput(), > > 'password':PasswordInput(), > > 'usertype':Select(choices=(("t1","type1"), > > ("t2","type2"))), > > } > > > The form comes up as i wanted it to, but when i login it gives a > > validation error saying "user with this name already exists". > > I think its "form.is_valid()" checking "primary key" constraint, > > thinking i'm trying to write into the table. :( > > I cant remove the "primary key" constraint on "username" is there a > > way around this problem.. > > Why do you want a modelform though? The usual reason is to allow you > to add users or edit existing ones - since you don't supply an > existing user when instantiating the form, Django assumes you want to > create a new one, hence the error. But what are you gaining by using > the modelform here? It seems to me, nothing at all. Instead of > subclassing modelform, setting the model, and defining the widgets, > you might as well just use a normal form and define the fields > directly. > -- > DR.
Hi Daniel, Yeah I had a thought like that. I thought I could leave the validation to the model. Turns out model form comes handy only if your modifying or updating something in the database, as you said. Simple form can also enforce constraints but I just have to write another form with same constraints as the model. Thanks for the suggestion. Warm Regards, Pranav. -- 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.

