1

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 
5
  • 1
    lots of things wrong with this question. Where is your modelform. What about the indentation. What on earth is presciptiontemplates which appears to be undefined? Commented Jan 5, 2017 at 6:39
  • Show the prescription.html and also tell us what is presciptiontemplates ? Commented Jan 5, 2017 at 6:46
  • Added modelform , presciptiontemplates is the model in which I Intend to save the Data from Rich text Editor Commented Jan 5, 2017 at 7:03
  • Add full traceback of the error. The error is coming from forms.py or from this line presciptiontemplates.patientid = userid ? Commented Jan 5, 2017 at 7:11
  • Error is Caused due to form.save() In views.py Commented Jan 5, 2017 at 7:19

3 Answers 3

2

The problem is in this line :

presciptiontemplates.patientid = userid

In your model, patientid is a foreign key to Patient Model, So it is expecting a Patient model object, and not the id of the patient as patid. So firstly get the Patient object and then assign it to patientid,Like this :

def viewtemplate(request, patid):

    patient = Patient.object.get(id=patid)

    form = templateform(request.POST)
    if request.method == "POST":
        if form.is_valid():
            form = form.save(commit=False)
            form.patientid = patient
            form.save()
        return redirect('index')
    else:
        form = templateform()
    return render(request, 'presapp/prescription.html', {'form': form})
Sign up to request clarification or add additional context in comments.

8 Comments

It gives me an error int() argument must be a string, a bytes-like object or a number, not 'Patient' Any suggestions
@RomilBahukhandi Error is in which line of this code ?
@RomilBahukhandi updating my answer. Try form.patientid = patid in the place of patient object.
The error trace-back is to form.save() Im Using MSSQL As the backend using Pyodbc-azure , Can it be the Problem
Use this line also form = form.save(commit=False) Then the error should be gone.
|
2

Assuming templateform is a model form.

You can use commit=False parameter in the save method and then save the object again with required parameters.

Short snippet to change as per your code

...
if request.method == "POST":
    if form.is_valid():
        obj = form.save(commit=False)
        obj.patientid = userid
        obj.save()
    return redirect('index')
else:
...

Comments

0

To add userid value into new object you can use ModelForm.

Just make templateform inherited from ModelForm not Form class.

class templateform(forms.ModelForm):
    class Meta:
        model = presciptiontemplates

now in view you can do following:

form = templateform(request.POST or None, request.FILES or None)
if form.is_valid():
    instance = form.save(commit=False)
    instance.patientid = userid
    instance.save()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.