0

Okay, so I'm trying to teach myself django by trying to put together a simple DB query application.

So I have in my DB a relation mysql table storing triples (RDF, subj, obj, pred) and I have written a model form with the fields to query that. Though, I have initially setup my form to store the queries in a separate table. What I would like to do however, is the model form I created to instead query the triple table. Here is my code:

view:

from django.shortcuts import render, get_object_or_404, render_to_response
from django.template import RequestContext

# Create your views here.
from .forms import QueryForm
from .models import Query

def queries_create(request):
    form = QueryForm(request.POST or None)
    if form.is_valid():
        instance = form.save(commit=False)
        instance.save()

    context = {

        "title":"Create",
        "form": form,

    }
    #return HttpResponse("<h1>create</h1>")
    return render(request, "query_form.html", context)

model:

from __future__ import unicode_literals

from django.db import models
from django.core.urlresolvers import reverse

# Create your models here.

class Query(models.Model):
    studyName = models.CharField(max_length=250)
    population = models.IntegerField()
    intervention = models.CharField(max_length=250)
    comparison = models.CharField(max_length=250)
    outcome = models.CharField(max_length=250)
    outcomeTiming = models.CharField(max_length=250)

    updated = models.DateTimeField(auto_now=True, auto_now_add=False)
    timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)

    def __unicode__(self):
        return self.studyName


    def get_absolute_url(self):
        return reverse("queries:detail", kwargs={"id": self.id})
        #return "/queries/%s/" %(self.id)

form:

from django import forms

from .models import Query

class QueryForm(forms.ModelForm):
    class Meta:
        model = Query
        fields = [

            "studyName",
            "population",
            "intervention",
            "comparison",
            "outcome",
            "outcomeTiming",

    ]

html:

<!--DOCTYPE html -->

<html>
<body>
<h1>Query the Model</h1>

<form method='POST' action=''>{% csrf_token %}
{{  form  }}
<input type='submit' value='Query!' />
</form>

</body>
</html>

Any help would be appreciated I've tried several modifications but nothing seems to be working.

jdv12
  • 171
  • 4
  • 17
  • You need to let us know what error do you have. `nothing seems to be working` is not enough for us to debug it for you. – Shang Wang Feb 17 '16 at 16:33
  • This code block works but inserts the form data into its own DB. However, what I would like to do is instead of POST I would like to search the DB by keyword and return matching files. – jdv12 Feb 17 '16 at 17:09

1 Answers1

1

You need to handle the data differently for the form. Instead of saving it you need to extract the data and query for matching:

def query_queries(request):
    form = QueryForm(request.POST or None)
    if form.is_valid():
        # this is the same as doing
        # Query.objects.filter(studyName=form.cleaned_data['studyName']...)
        queries = Query.objects.filter(**form.cleaned_data)

    context = {
        'queries': queries
    }
    return render(request, "query_queries.html", context)
Shang Wang
  • 24,909
  • 20
  • 73
  • 94
  • Okay that makes sense although I'm not sure what you mean by: "**form.cleaned_data" ? – jdv12 Feb 17 '16 at 17:34
  • It's python syntax for feeding a dictionary as parameters: http://stackoverflow.com/questions/334655/passing-a-dictionary-to-a-function-in-python-as-keyword-parameters – Shang Wang Feb 17 '16 at 18:21
  • I don't understand how come this answer is right here the argument `queries` is references in `None` case before assignment. – Nitish Kumar Pal Jul 10 '18 at 10:10