Observação: estou usando o urlnorm , mas isso
é apenas para mostrar onde conectar a lógica – você pode querer usar
outra coisa.
models.py
from django.db import models
class LinkyDink(models.Model):
url = models.URLField()
admin.py
from urlnorm import norm
from django import forms
from django.contrib import admin
from models import LinkyDink
class LinkyDinkForm(forms.ModelForm):
def full_clean(self):
if self.data:
field_name = 'url' if not self.prefix
else '{}-url'.format(self.prefix)
field_value = self.data.get(field_name)
if field_value:
self.data[field_name] = norm(field_value.strip())
super(LinkyDinkForm, self).full_clean()
class Meta:
model = LinkyDink
class LinkyDinkAdmin(admin.ModelAdmin):
form = LinkyDinkForm
# ... etc.
# also works with inlines (because of the prefix logic)...
class LinkyDinkInline(admin.TabularInline):
model = LinkyDink
form = LinkyDinkForm
# ... etc.