Django model formset to update objects with user input

Having some troubles with understanding objects update using model formset. Creation of objects is not a problem as flow is clear. Updating predefined objects is not a problem as well as we use instance to pass instances. However the question is : How to update objects which user inputs in model formset fields?

Let's say I have some dummy code:

models:

class Product(models.Model):
    serial_number = Charfield()
    config = ForeignKey(Configuration)

class Configuration(models.Model):
    items = ManyToMany(Item)

forms:

class ProductFormset(forms.BaseModelFormSet):
    def __init__(self, *args, **kwargs):
        super(ProductFormset, self).__init__(*args, **kwargs)

        self.queryset = models.Product.objects.none()


class ProductForm(forms.ModelForm):
    class Meta:
        model = Product
        fields = ["serial_number"]

ProductFormSet = modelformset_factory(Product, ProductForm, ProductFormset, min=1, extra=0)

So whenever user inputs some serial_number(s) on creation page I'm able to use clean_data to process new objects and save(). Whenever I use object's DetailView to redirect to update link I'm able to pass object id and use it to assign as instance and all works fine for save(). But on update page I want to let user decide which products to edit. So until form submitted the serial_number(s) are unknown and can't be used as initial. Formset is_valid() returns False as well as field error risen Object already exists. I'm also using JS to dynamically add new forms utilizing formset.empty_form. Currently I switched to normal Formset but it feels wrong as I have to do all validations manually (for example checks if object exists in DB). What I'm missing?

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