I have 3 models: Human, Human_notes, and Location; creating 3 forms. All three are in one template as a ‘single form’ submitted at one time. I am trying to get the given primary key values from 2 of the form submissions and use these as the foreign keys on the 3rd form. BUT this does not work the way I am doing it… why? Is this the general idea of how you would do this? I get an error saying:
‘LocationForm’ object has no attribute ‘id’ though I have explicitly set that in the form and models.
oh, and the other form data IS saved… just no linking keys.
Thanks in advance…
Here is my template:
<form action="human-add" method="POST">
{% csrf_token %}
{{ human_form.as_p }}
{{ location_form.as_p }}
{{ human_notes_form.as_p }}
<button type="submit" class="save btn btn-success">Save</button>
</form>
Here is my view:
def human_add(request):
if request.method == 'POST':
human_form = HumanForm(request.POST)
location_form = LocationForm(request.POST)
human_notes_form = HumanNotesForm(request.POST)
if human_form.is_valid() and location_form.is_valid() and human_notes_form.is_valid():
human_form.save(commit=False)
location_form.save()
locationKey = location_form.id
human_notes_form.save(commit=False)
human_notes_form.intaker = request.user
human_notes_form.save()
noteKey = human_notes_form.id
human_form.location = locationKey
human_form.note = noteKey
human_form.intaker = request.user
human_form.save()
return redirect('/intake/success-form')
else:
context = {
'human_form': human_form,
'location_form': location_form,
'human_notes_form': human_notes_form,
}
else:
context = {
'human_form': HumanForm(),
'location_form': LocationForm(),
'human_notes_form': HumanNotesForm(),
}
return render(request, 'intake/human-add.html', context)
Here is the Human model piece:
class Human(models.Model):
intaker = models.ForeignKey(User, default=None, on_delete=models.CASCADE, null=True)
location = models.OneToOneField('Location', on_delete=models.CASCADE, null=True, related_name='humans')
Here are my forms:
class HumanForm(forms.ModelForm):
class Meta:
model = Human
widgets = {'pwd': forms.PasswordInput(),'roles': forms.SelectMultiple(attrs={'size': 2}),}
fields = ['intaker','roles','fnm','lnm','phn1','phn1_txt','phn2','phn2_txt','eml1','eml2','pwd','rcvr_code','alt_fnm','alt_lnm','alt_phn','cart_id','auth_care','img','img_id'] #"__all__"
class HumanNotesForm(forms.ModelForm):
class Meta:
model = HumanNotes
fields = ['id','intaker','note']
class LocationForm(forms.ModelForm):
class Meta:
model = Location
fields = ['id','st1','st2','cty','county','state','zip','lat','long','img','img_nm','img_id']
wazup?