1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | class UserField(forms.CharField):
class widget(forms.widgets.TextInput):
def render(self, name, value, attrs=None):
if isinstance(value, int):
value = unicode(User.objects.get(pk=value))
return super(UserField.widget, self).render(name, value, attrs)
def clean(self, value):
value = super(UserField, self).clean(value)
if not value:
return None
try:
return User.objects.get(username=value)
except User.DoesNotExist:
raise forms.ValidationError(u'invalid user name')
|
More like this
- PreSaveMiddleware by pterk 5 years, 6 months ago
- UserForeignKey by hawkeye 5 years, 2 months ago
- ISBN model field: displays 10- and 13-digit variants and external links by fish2000 3 years, 1 month ago
- PatchModelForm - A ModelForm subclass with the semantics of the PATCH HTTP method by gnrfan 11 months, 3 weeks ago
- Duplicate related objects of model instance by johnboxall 4 years, 4 months ago
Comments