Hi,
In the admin panel, I created a form to add a product. The form includes 2 built-in sets of forms, as there are some models associated with the product. The user can create a Product, and then define variants of this product that have different attributes. I will illustrate this with an example. The user has a t-shirt of the same brand in 3 different colors and wants to add them at different prices. The t-shirt is created as a Product with 3 options.
class Detail(models.Model): product = models.ForeignKey('Product') attribute = models.ForeignKey('Attribute') value = models.CharField(max_length=500) class Attribute(models.Model): name = models.CharField(max_length=300) class Variant(models.Model): product = models.ForeignKey(Product) details = models.ManyToManyField(Detail) quantity = models.IntegerField() price = models.DecimalField(max_digits=6, decimal_places=2)
I skipped the product as it doesn't matter.
class DetailInline(admin.TabularInline): model = Detail class VariantInline(admin.StackedInline): model = Variant class ProductAdmin(admin.ModelAdmin): class Meta: model = Product inlines = [DetailInline, VariantInline]
This works well, models persist well, I have a problem with inline options. Embedded display options Detailed objects, but only those that are already stored in the database. To make life easier for the user, it would be better to add Detail objects to the Variant inline when creating Detail objects, so this should happen before the Product is saved.
- Is there a way to manually update Inline with values?
- Is there an average value that I could use to create Detail objects, but not Product objects, and get back to the results?
- Should I redo the model? (I really don't want to do this unless I want to)
- Is there any other workflow that the user will have to use to add the product?
I tried to insert records into a string using js, but this is hacky, and Django did not check the form set with false values, throwing an error that selected the wrong value.
The final thoughts that came to my mind when I wrote this question. You can create js so that if the Changed shape of the object has changed, data will be transferred to the user view, which would create the objects and return with the results. One of the problems that I see (next to it is not very good) is how to tell django to create new objects so that it does not cause an error about nonexistent values.
In any case, I hope someone understands this long question.