Opened 9 years ago

Closed 9 years ago

#26131 closed Bug (invalid)

model field choices are not enforced

Reported by: stiffupperlip Owned by: nobody
Component: Database layer (models, ORM) Version: 1.8
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by stiffupperlip)

In Django 1.8.8 when I create a model class like below, choices are not enforced when creating an instance of the model class:

class HTTPMethod(models.Model):
    HTTP_METHODS = [(_, _) for _ in ['get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace', 'connect']]

    id = models.AutoField(primary_key=True)
    name = models.CharField(choices=HTTP_METHODS, unique=True, max_length=255)


h = HTTPMethod(name='something invalid')
h.save()

No errors are raised and invalid data is stored in the model.

Python 2.7.6 on Ubuntu 14.04

Change History (4)

comment:1 by stiffupperlip, 9 years ago

Description: modified (diff)

comment:2 by stiffupperlip, 9 years ago

Description: modified (diff)

comment:3 by stiffupperlip, 9 years ago

Description: modified (diff)

comment:4 by Tim Graham, 9 years ago

Resolution: invalid
Status: newclosed

As described in the validation documentation,

Note that full_clean() will not be called automatically when you call your model’s save() method. You’ll need to call it manually when you want to run one-step model validation for your own manually created models.

Note: See TracTickets for help on using tickets.
Back to Top