Django Admin: Update Inline Based on Other Inline

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.

+6
source share
2 answers

One thing that comes to mind is Knockout.js .

This is very convenient when updating a large number of elements in the DOM at the same time, and you can easily return new values ​​to your custom view using Ajax calls from client events.

There are a few frameworks that can do this, but I think Knockout is one of the easiest to read and implement the most popular ones like Backbone, Angular, Ember, etc.

Django usually complains about dynamically added options, but as long as they exist on the server side, when the form is validated, theoretically you should be fine.

+2
source

I decided to abandon this idea, as it took a lot of time and would probably take more. And what I came up with was a rather nasty hack than coding. But instead of leaving it hanging here, I will post any hints to other people.

I have a model with two different models as inline forms, both of them have the foreign key of the main model, and one of them has the foreign key of the other. The idea was to create fake entries in one of the lines, depending on the values ​​entered by the user. As shown, this was pretty easy to do with jQuery, which comes with Django. So I did, but, of course, Django knew that this model did not exist. The solution was to create my own form and field and override the clean () method, as I did here .

This led to many problems, some of which were that my models depended on each other and that the clean () method of the field had to be shortened a bit so as not to check the existence of the db object. Addition to this was the lack of information about POST data at the stage of checking the field, so the clean () method of the form had to be redefined because the data was published. However, it was unclean, so it had to be removed from the POST test and verified. At this point, I decided to stop because it was all the more complex and could lead to inconsistent data. I assume that the next step will be to override the ModelAdmin saving method, since the created object is not in the set of queries bound to the set of forms, unless there is a place somewhere in the path where this can be done.

To summarize this, I have to say that at this point I would never do it this way and go with my own change_form view to better control the flow of data.

TL; dg

Create a custom view of change_form.

0
source

All Articles