weird behavior while saving two forms

Hi. Im fairly new to django, and I see this behavior for the first time.
So, basically, i have 2 model forms, one for parent and one for child model.
Both forms created with passing model instances to them.
In my view, when i do
parent = parent_form.save()
child = child_form.save()
parent object fields not updated in DB (save method returns object with properly updated fields though), but when i do another saving order, like this:
child = child_form.save()
parent = parent_form.save()
then all works fine.
Any suggestions what i do wrong?

The quick answer is that when you have multiple forms on a page, you need to use a prefix on your forms to prevent field conflicts.

If this isn’t the issue, then we’re likely going to need to see the forms and the view involved.

When posting blocks of code here, enclose the code between lines of three backtick - ` characters to force the forum software to maintain the formatting of the code. You’ll have a line of ```, then the code, then another line of ```. (Don’t post images, copy/paste the code into your reply.)

thanks for response.
code with unexpected behavior looks like that:

parent_form = forms.ParentForm(request.POST, prefix='parent', instance=parent)
child_form = forms.ChildForm(request.POST, prefix='child', instance=child)

if parent_form.is_valid() and child_form.is_valid():
	with transaction.atomic():
		parent = parent_form.save()
		child = child_form.save()
		
		# here i see correct field value
		# but in DB field isn't updated
		print(parent.example_field)

But when i just change forms saving order, all works as expected

parent_form = forms.ParentForm(request.POST, prefix='parent', instance=parent)
child_form = forms.ChildForm(request.POST, prefix='child', instance=child)

if parent_form.is_valid() and child_form.is_valid():
	with transaction.atomic():
		child = child_form.save()
		parent = parent_form.save()

I think we are also going to need to see the forms and models, too. What is the relationship between the two? (Are there foreign keys involved here?) Is this an update or create process? (This isn’t the complete view - we need to see more.)

Its 1-to-1 kinda relationship (something like user-profile etc). There is multi-table inheritance between models.
Its update process (so both objects already in DB, im just updating some fields) Yes, i know i wouldnt be able to save child object first without foreign key value from parent, but this is not that kind of sutuation
Im not sure the code would tell something here, its pretty basic, im not showing fields, but i assure you nothing special there, just ordinary model fields

# models

class Parent(models.Model):
    
    id = models.AutoField(primary_key=True)
    # other fields 
    
    class Meta:
        db_table = 'parent_table'	
		
class Child(Parent):
    # fields here
    
    class Meta:
        db_table = 'child_table'

class ParentForm(forms.ModelForm):
	
    class Meta:
        model = Parent
        fields = [
            # fields here
        ]


class ChildForm(forms.ModelForm):
	
    class Meta:
        model = Child
        fields = [
            # fields here
        ]

So, my questions here:

  1. why i see correct field value when im printing parent object’s field after saving it, but in DB field isnt updated. Meantime all fields from child object are updated properly.
  2. why its working fine when i change forms saving order to saving child form first

I can’t answer these questions without getting a complete picture of what you’re trying to do. You may think you’re trimming out unimportant information, but I’m not willing to make that assumption - especially since I don’t see anything wrong with what you’ve posted so far.