from .models import Page class AutoPageConverter: regex='.*' def to_python(self,value): try: p=Page.objects.get(url=value) except Page.DoesNotExist: raise ValueError(f'No such page {value}') if not p: raise ValueError(f'No such page {value}') return p def to_url(self,value): if not isinstance(value,Page): raise ValueError('Given value is not a Page') return p.url