views:

385

answers:

2

I need a model field composed of a numeric string for a Django app I'm working on and since one doesn't exist I need to roll my own. Now I understand how "get_db_prep_value" and such work, and how to extend the Model itself (the django documentation on custom model fields is an invaluable resource.), but for the life of me I can't seem to figure out how to make the admin interface error properly based on input constraints.

How do I make the associated form field in the admin error on incorrect input?

A: 

Have a look at the Form and field validation section in the Django documentation, maybe that's what you're looking for?

You would have to make a new type of form field for your custom model field.

Blixt
But how do you integrate this with the admin interface?
akdom
Have a look at this documentation: http://docs.djangoproject.com/en/dev/topics/forms/modelforms/
Blixt
You should also read this for how to specify what form to use in the admin: http://docs.djangoproject.com/en/dev/ref/contrib/admin/
Blixt
A: 

All you need to do is define a custom modelform which uses your new field, and then tell the admin to use that form to edit your models.

class MyModelForm(forms.ModelForm):
    myfield = MyCustomField()

    class Meta:
        model = MyModel


class MyModelAdmin(admin.ModelAdmin):
    form = MyModelForm
Daniel Roseman