开发者

django update only one field of a model through a form

开发者 https://www.devze.com 2023-03-08 22:04 出处:网络
I have this models: class Balanta(models.Model): data = models.DateField() class Conturi(models.Model): cont=models.PositiveIntegerField()

I have this models:

class Balanta(models.Model):
    data = models.DateField()

class Conturi(models.Model):
    cont=models.PositiveIntegerField()
    cont_debit=models.DecimalField(default=0, max_digits=30, decimal_places=2)
    cont_credit=models.DecimalField(default=0, max_digits=30, decimal_places=2)
    开发者_如何学Cbalanta = models.ForeignKey(Balanta)

And i have formsets working ok in a template and this view:

def balanta_introducere(request):
    balanta=Balanta()
    ConturiInlineFormSet=inlineformset_factory(Balanta, Conturi, extra=3)
    if  request.method=='POST':
        balanta_form=BalantaForm(request.POST, instance=balanta)
        if balanta_form.is_valid():
            balanta, created=Balanta.objects.get_or_create(**balanta_form.cleaned_data)
            #return HttpResponseRedirect('/sitfin/balantaok')
        formset=ConturiInlineFormSet(request.POST, request.FILES, instance=balanta)
        if formset.is_valid():
            for form in formset:
                data={
                        'cont':form.cleaned_data.get('cont'),
                        'cont_debit':form.cleaned_data.get('cont_debit'),
                        'cont_credit':form.cleaned_data.get('cont_credit'),
                        'balanta':form.cleaned_data.get('balanta'),
                }
                try:
                    c=Conturi.objects.get(cont=data['cont'])
                except Conturi.DoesNotExist:
                    cont_complete,created=Conturi.objects.get_or_create(**data)
                else:
                    cont_complete,created=Conturi.objects.get_or_create(cont=data['cont'],cont_debit=data['cont_debit'],cont_credit=data['cont_credit'],balanta=data['balanta'])
    else:
        balanta_form=BalantaForm()
        formset=ConturiInlineFormSet(instance=balanta)
    return render_to_response('sitfin/balanta_introducere.html',{'balanta_form':balanta_form,'formset':formset}, context_instance=RequestContext(request))
  1. If i hit the first submit, all the data goes in the database (foreignkey and all)
  2. After the second submit with the same data, the form doesn't do anything and this is ok.
  3. If i change a value in the form (in a "cont_credit" of a "cont" for example) and hit submit again, i get another Conturi object with only the modified "cont" with the updated "cont_credit" value and this is not good!

What is the approach for updating only some fields of an existing Conturi model with the help of a form?

Something like:

If the cont it is not in the database,
create a Conturi objects with the data in the form,
If the "cont" is already in the database,
Update the cont_credit and cont_debit data with the new values entered in the form

Thank you very much.


get_or_create is trying a get with all the parameters you pass it, so if anything changes on the form, it won't find the existing object, and instead will create a new one.

If your forms are ModelForms, then you can just use form.save() to save the instance bound to the form, and formset.save() to save all the instances bound to the formset.

EDIT:
I now noticed another thing: you are using

balanta=Balanta()

and then

balanta_form=BalantaForm(request.POST, instance=balanta)

so you are forcing the form to use a new instance. try getting the specific Balanta you're editing, and pass that as the instance.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号