Django JSONFIeld's widget for data input in format: forms.Select (key) - forms.TextInput (value)

I have a hard times to implement "user friendly" JSON input within a form.

I have some Item model which contains attributes = JSONFIeld().

class ItemType(models.Model):
    title = models.CharField()

class Item(models.Model):
    title = models.CharField()
    item_type = models.ForeignKey(ItemType)
    attributes = models.JSONField()

To keep particular "schema" within a single ItemType I've added models:

class ItemAttribute(models.Model):
    title = models.CharField(max_length=100, unique=True)

class ItemAttributeSpec(models.Model):   
    item_type = models.ForeignKey(ItemType)  
    attribute = models.ForeignKey(ItemAttribute)  
    required = models.BooleanField(default=False)
    choices = models.JSONField(default=list)

So a goal for implementation: Provide a set of attributes' key/value pairs where key of JSON field will be forms.Select() or just label (doesn't matter much as I can manage implementation of this feature) and value is an input. So every single Item form instance would has all type-related attributes for input. Generally some kind of formset within a single form instance.

Something like this: enter image description here

Вернуться на верх