jrief/django-entangled

Can you use the same form and template for multiple objects with different JSON strings?

Opened this issue · 0 comments

In the example on the front page, a form is created to explicitly match the properties that a given object would have. i.e. the properties "color", "size", and "tenant" are explicitly added to a form.

Do you need to create a separate form for each product type that might be created?

In my current setup, I have:

class LineItem(models.Model):
  product_type = models.ForeignKey(ProductType, on_delete=models.CASCADE)
  quantity = models.IntegerField()
  specs = models.JSONField()


class ProductType(models.Model):
    product_name = models.TextField()
    specs = models.JSONField()

In the template for creating a new lineitem, I use AJAX so that:

  • The user selects a ProductType for their new LineItem
  • The specs field from the ProductType is copied to the specs field for the LineItem
  • A label and a textarea are created for each key:value pair in the LineItem specs field.

It works, but it's a hack.

To take it back to the language in your example, is there be a way to use a single template/form for creating, e.g. a t shirt with "size" and "color" properties and a pair of pants with "waist", "length", and "color" properties?