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.

2
  • 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. Commented Feb 17, 2016 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. Commented Feb 17, 2016 at 17:09

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

3 Comments

Okay that makes sense although I'm not sure what you mean by: "**form.cleaned_data" ?
It's python syntax for feeding a dictionary as parameters: stackoverflow.com/questions/334655/…
I don't understand how come this answer is right here the argument queries is references in None case before assignment.

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.