one form field not setting in db...

I have a CRUD site and a model form (PenForm). One field (shelter) is a foreign key to another model. It shows on the form and all other fields are submitting and show in the DB - except this one. The field shows on the form as a select list drop down and source HTML looks ok. Any help appreciated…

Model:

class Pen(models.Model):
    shelter = models.ForeignKey(Shelter, on_delete=models.CASCADE, null=True)
    intaker = models.ForeignKey(User, default=None, on_delete=models.CASCADE, null=True)
    child = models.ForeignKey(human, default=None, on_delete=models.CASCADE, null=True)
...

Form:

class PenForm(forms.ModelForm):
    class Meta:
        model = Pen
        fields = ['shelter','occupied',...] 

View:

@login_required
@transaction.atomic
def child_add(request, id):
    ownr = User.objects.get(id=id)
    fnm = ownr.first_name
    lnm = ownr.last_name
    loc = ownr.profile.location
    if request.method == 'POST':
        human_form = HumanForm(request.POST, request.FILES)
        pen_form = PenForm(request.POST)
        if human_form.is_valid() and pen_form.is_valid():  
            usr_curr = User.objects.get(id=id) 
            prf_curr = Profile.objects.get(user_id=usr_curr.id)   
            prf_locid_curr = prf_curr.location_id  
            prf_loc_curr = prf_curr.location_id
            human_form.save(commit=False)
            human_form.instance.intaker = request.user 
            human_form.instance.human = usr_curr 
            human_form.instance.location = prf_curr.location 
            human_form.save()
            hum_curr = Human.objects.last()
            messages.success(request, f"Human data was saved successfully.")
            pen_form.save(commit=False)
            pen_form.instance.intaker = request.user
            pen_form.instance.human = hum_curr 
            #pen_form.instance.shelter = pen_form.shelter  # tried to explicitly add though should not have to...
            pen_form.save()
            return redirect('/intake/humans')
    else:
        human_form = HumanForm()
        pen_form = PenForm()
        context = {
            'id': id,
            'fnm': fnm,
            'lnm': lnm,
            'human_form': human_form,
            'pen_form': pen_form,
        }
    return render(request, 'intake/human-add.html', context)

Thoughts? Any clues appreciated…

Not quite enough info here for me to feel confident with making a full determination, but there is one thing I did notice…

When working with a Model Form and using the save(commit=False) option, notice how the documentation shows that the method returns the new instance of the model being saved, and subsequent work is performed on the model instance and not the form, or even the ‘instance’ within the form.

Beyond that, when you have multiple forms on one page, Django needs some way to associate the data with each form. See the Prefixes for Forms docs.

Ken,
As always thanks for being out there with advice and assistance… much appreciated!

So, I had not added all models in above description, but I use two models/tables and each had a FK to shelter using the same var (‘shelter’). Your prefix suggestion made me think maybe that would cause a conflict in Django - though in my case I don’t think the prefix stuff is applicable (my read is it is more for multiple copies of the SAME form). HOWEVER, it got me thinking and - DUH - I was looking for the data in the wrong table… my bad. It WAS in the OTHER table and I just needed to copy from the instance of the ‘wrong’ (Pen) model into the right (Human)…

My apologies and profuse thanks. I would have likely struggled more without your input making me consider another angle.

Glad you’ve got a solution.

Nope, it’s for any situation where you have multiple forms on the same page. It’s not always necessary. If there are no conflicts with field names (and therefore html IDs or form field submission names), you’re fine without it. However, as soon as you have the same name in both models, one will be obscured. We’ve always considered it safer to use a prefix with the multiple forms - having been bitten in the past by adding a field and spending far too much time finding out why we were seeing strange results. :grin: