surenkov/django-pydantic-field

Support `Tuple[]` and `NamedTuple` types

pirate opened this issue · 0 comments

This is a separate issue to add NamedTuple/Tuple support, broken out from #65

I have this Pydantic model that I want to allow users to edit in the UI:

from typing import NamedTuple

class SemVer(NamedTuple):
    major: int
    minor: int = 0
    patch: int = 0
    
    def __new__(cls, *args, **kwargs):
		# allow creating from string SemVer('1.2.3') or SemVer(1, 2, 3)
		if len(args) == 1 and isinstance(args, str):
			args = (int(chunk) for chunk in args[0].split('.'))
        return cls(*args, **kwargs)
	
class Dependency(models.Model):
    min_version: SemVer = SchemaField(default=(0,0,1))

Currently trying to render the Admin UI produces a number of errors in the UI, but the key one for this issue is:
image


(!) Error: Error while creating EditorState: Invalid schema: Schema of type 'array' must have a key called 'items'

I have also seen this error when trying to nest SemVer inside a different Pydantic BaseModel:
image