how do I use CommaSeparatedIntegerField in django?


Question

There is almost no documentation for it here: http://docs.djangoproject.com/en/dev/ref/models/fields/#commaseparatedintegerfield

Maybe someone could, by example, show me how to populate a CommaSeparatedIntegerField in Django?

-thanks.

1
38
2/27/2012 11:15:35 AM

Looking at the source for django...

class CommaSeparatedIntegerField(CharField):
    def formfield(self, **kwargs):
        defaults = {
            'form_class': forms.RegexField,
            'regex': '^[\d,]+$',
            'max_length': self.max_length,
            'error_messages': {
                'invalid': _(u'Enter only digits separated by commas.'),
            }
        }
        defaults.update(kwargs)
        return super(CommaSeparatedIntegerField, self).formfield(**defaults)

Check out that regex validator. Looks like as long as you give it a list of integers and commas, django won't complain.

You can define it just like a charfield basically:

class Foo(models.Model):
    int_list = models.CommaSeparatedIntegerField(max_length=200)

And populate it like this:

f = Foo(int_list="1,2,3,4,5")
22
7/30/2009 7:34:00 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon