PauliusZ
PauliusZ

Reputation: 413

Django admin field class override for validation

I am trying to implement phone value validation in Django admin. For this I want to use already existing Field class localflavor.lt.forms.LTPhoneField. But with the simplified code example bellow validation does not work:

# from model.py
class Person(models.Model):
    name = models.CharField(max_length=50)
    phone = models.CharField(max_length=15)

# from admin.py
from django.contrib import admin
from localflavor.lt.forms import LTPhoneField

class PersonAdmin(admin.ModelAdmin):
    phone = LTPhoneField()
admin.site.register(Person, PersonAdmin)

Edit: Solved. See my own answer.

Upvotes: 0

Views: 278

Answers (2)

PauliusZ
PauliusZ

Reputation: 413

I solved this by adding additional forms.ModelForm class like this:

class PersonForm(forms.ModelForm)
    phone = LTPhoneField()

class PersonAdmin(admin.ModelAdmin):
    form = PersonForm

admin.site.register(Person, PersonAdmin)

Upvotes: 1

Priyank Patel
Priyank Patel

Reputation: 3535

Try some thing like :

class PersonAdmin(admin.ModelAdmin):
    phone = LTPhoneField()
    def clean_phone(self):
        return self.cleaned_data["phone"]
admin.site.register(Person, PersonAdmin)

**Not Tested

Upvotes: 0

Related Questions