Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#3729 closed (worksforme)

For integer field inside is provided string value when 'choices' option is used

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


I found that inside save() method of model field declared as PositiveSmallIntegerField actually is string! So instead 2 I have "2" as self.status value. If 'choices = STATUS_LIST' will be removed from field declaration bug will disappear and inside of save() I have integer as it should be.

from django.db import models
import types

     (1, 'New'),
     (2, 'Public'),
     (4, 'Hidden'),

class Dummy(models.Model):
     name = models.CharField(maxlength=16)
     status = models.PositiveSmallIntegerField(default = 1, choices = STATUS_LIST)
     class Admin:

     def __str__(self):

     def save(self):
          print " status=%s, type=%s" % (self.status, type(self.status))
          assert type(self.status) == types.IntType, \
              "instead of IntType received %s, value is %s" % (type(self.status), self.status)

          super(Dummy, self).save()

Change History (3)

comment:1 Changed 11 years ago by Robert Coup

This isn't technically a problem in the models (well, I can't reproduce it using your code). They don't currently do any validation (including limiting to choices or even checking whether things are integers). It's a big work-in-progress to get model validation happening.

However, if data comes back from a newforms.ChoiceField it's always a string, not whatever type is in the choices list.

comment:2 Changed 11 years ago by Philippe Raoult

Resolution: worksforme
Status: newclosed

comment:3 Changed 11 years ago by Robert Coup

See #5481 for the relevant fix to ChoiceField

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