tags:

views:

1121

answers:

2

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 commaseperatedintegerfield in django?

-thanks.

+3  A: 

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")
Triptych
A: 

I just happened to have dealt with CommaSeparatedIntegerField in my latest project. I was using MySQL and it seemed like supplying a string of comma separated integer values is very DB friendly e.g. '1,2,3,4,5'. If you want to leave it blank, just pass an empty string.

It does act like a CharField, and beaved in a weird way for me.. I ended up with values like "[1, 2, 3, 4, 5]" including the brackets in my database! So watch out!

tarequeh