Hello, I wrote a custom field and I would like to have a proper widget in admin.
As you may guess reading my code I would like to manage that field with a multiselect, but instead I get a regular select in admin. How should I do it? (relevant part should be definition of formfield) #-------------------------------------------- from django.db import models from django.core import validators from django import newforms as forms class MultipleChoiceField(models.TextField): __metaclass__ = models.SubfieldBase def __init__(self, *args, **kwargs): if 'separator' in kwargs: self.separator = kwargs.pop('separator') else: self.separator = '|' for choice in kwargs['choices']: if self.separator in choice[0]: raise TypeError('Separator \'%s\' colliding with choices, see choice \'%s\'.' % (self.separator, choice[0])) super(MultipleChoiceField, self).__init__(*args, **kwargs) def formfield(self, **kwargs): defaults = {'form_class': forms.ModelMultipleChoiceField, 'widget': forms.SelectMultiple} defaults.update(kwargs) return super(MultipleChoiceField, self).formfield(**defaults) def get_internal_type(self): return "TextField" def to_python(self, value): if value is None: if self.null: return value else: raise validators.ValidationError, ugettext_lazy("This field cannot be null.") return str(value).strip(self.separator).split(self.separator*2) def get_db_prep_save(self, value): if type(value) == type([]): value = list(set(value)) elif type(value) == type(()): value = list(value) elif type(value) == type(''): value = [value] elif type(value) == type(0): value = [str(value)] else: raise TypeError('Lookup value type %r not supported.' % type(value)) value.sort() return self.separator + (self.separator*2).join(value) + self.separator def get_db_prep_lookup(self, lookup_type, value): if lookup_type == 'exact': return self.get_db_prep_save(value) if lookup_type == 'contains': value.sort() return '%' + '%'.join([self.separator + v + self.separator for v in value]) + '%' else: raise TypeError('Lookup type %r not supported.' % lookup_type) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---