This commit is contained in:
2022-01-27 20:59:32 +11:00
parent 014b17e507
commit 51661aaa71
3 changed files with 151 additions and 0 deletions

12
main/converters.py Normal file
View File

@@ -0,0 +1,12 @@
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