I'm Using a Form to save data into it.
models.py
class presciptiontemplates(models.Model):
templateid = models.AutoField(primary_key=True)
template = tinymce_models.HTMLField()
draft = models.BooleanField(default=False)
savedate = models.DateTimeField(default=datetime.now())
patientid = models.ForeignKey('Patient')
I'm Passing the parameter Patient Id From the URL.
url(r'^addprescription/(?P<patid>\d+)/$', views.viewtemplate
How do I save this parameter into form
views.py
def viewtemplate(request, patid):
userid = patid
form = templateform(request.POST)
if request.method == "POST":
if form.is_valid():
presciptiontemplates.patientid = userid
form.save()
return redirect('index')
else:
form = templateform()
return render(request, 'presapp/prescription.html', {'form': form})
forms.py
class templateform(forms.ModelForm):
class Meta:
model = presciptiontemplates
fields = ['template', 'draft']
widgets = {
'template': TinyMCE(),
'draft': forms.CheckboxInput()
}
labels = {
"patientid": _("Patient ID"),
}
def __init__(self, *args, **kwargs):
super(ThatForm, self).__init__(*args, **kwargs)
self.fields['template'].required = True
This gives me an error
[Microsoft][SQL Server Native Client 11.0][SQL Server]Cannot insert the value NULL into column 'patientid'
Django 1.10.4
Python 3.5.2
Windows 7