DSblizzard
DSblizzard

Reputation: 4149

Blank form fields are not accepted

Table in PostgreSQL database:

CREATE TABLE pmss_recordmodel
(
  id serial NOT NULL,
  "Name" character varying(100),
  CONSTRAINT pmss_recordmodel_pkey PRIMARY KEY (id)
)
WITH (OIDS=FALSE);
ALTER TABLE pmss_recordmodel OWNER TO postgres;

Model:

class RecordModel(models.Model):

    def __unicode__(self):
        return self.Name

    Name = models.CharField(max_length = 100, unique = False, null = True, blank = True)

When I POST data with blank Name field, form.is_valid() returns False. Why? Am I missing something?

EDIT:

class RecordForm(forms.ModelForm):

    class Meta:
        model = RecordModel

    Name = forms.CharField(label = "Имя ", widget = forms.TextInput(attrs = {'size': 15}))

Upvotes: 0

Views: 148

Answers (2)

j_syk
j_syk

Reputation: 6631

You could be having problems with how the field is defined in your database.

This seems like it could be the common situation of doing syncdb with the field initially not having blank=True null=True and then adding those terms later (after initial syncdb) to make the field not required. These settings will not be applied by simply running syncdb again. It requires a database migration or a database flush (will clear existing data, which isn't necessarily bad in development).

Upvotes: 0

jpic
jpic

Reputation: 33420

Django forms documentation:

By default, each Field class assumes the value is required

Set the "required" argument to False as such:

Name = forms.CharField(required = False, label = "Имя ", widget = forms.TextInput(attrs = {'size': 15}))

Upvotes: 1

Related Questions