Opened 18 years ago

Closed 17 years ago

Last modified 17 years ago

#3729 closed (worksforme)

For integer field inside Model.save() is provided string value when 'choices' option is used

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

Description

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

STATUS_LIST = (
     (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:
          pass

     def __str__(self):
          return self.name

     def save(self):
          print "Dummy.save 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 by Robert Coup, 17 years ago

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 by Philippe Raoult, 17 years ago

Resolution: worksforme
Status: newclosed

comment:3 by Robert Coup, 17 years ago

See #5481 for the relevant fix to ChoiceField

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