views:

354

answers:

1

MODEL:

class Pathology(models.Model):
    pathology = models.CharField(max_length=100)

class Publication(models.Model):
    pubtitle = models.TextField()

class Pathpubcombo(models.Model):
    pathology = models.ForeignKey(Pathology)
    publication = models.ForeignKey(Publication)
  1. List of pathology sent to HTML template as drop down menu

VIEW:

def search(request):
    pathology_list = Pathology.objects.select_related().order_by('pathology')
  1. User selects one pathology name from drop down menu and id retrieved by

VIEW:

def pathology(request):
    pathology_id = request.POST['pathology_id'] 
    p = get_object_or_404(Pathology, pk=pathology_id)

Where I'm stuck. I need the python/django syntax to write the following:

The pathology_id must now retrieve the publication_id from the table Pathpubcombo (the intermediary manytomany table). Once the publication_id is retrieved then it must be used to retrieve all the attributes from the publication table and those attributes are sent to another html template for display to the user.

+5  A: 

Hi, you should be using many-to-many relations as described here: http://www.djangoproject.com/documentation/models/many_to_many/

Like:

class Pathology(models.Model):
    pathology = models.CharField(max_length=100)
    publications = models.ManyToManyField(Publication)

class Publication(models.Model):
    pubtitle = models.TextField()

Then

def pathology(request):
    pathology_id = request.POST['pathology_id'] 
    p = get_object_or_404(Pathology, pk=pathology_id)
    publications = p.publications.all()
    return render_to_response('my_template.html',
                              {'publications':publications},
                              context_instance=RequestContext(request))

Hope this works, haven't tested it, but you get the idea.

edit:

You can also use select_related() if there is no possibility to rename tables and use django's buildin support.

http://docs.djangoproject.com/en/dev/ref/models/querysets/#id4

Tomh
The three tables were transferred from a PHP project. I think django creates the same manytomany table, but names it differently. The publication id must be retrieved through the pathpubcombo table. Is there anyway to write that in python?
I added a reference to what will be usefull for this to my answer
Tomh
I'll give it a try. Thanks!
I created a new table:class Pathology(models.Model): pathology = models.CharField(max_length=100) publication = models.ManyToManyField(Publication)
def onebigsel(request): pathology_id = request.POST['pathology_id'] p = get_object_or_404(Pathology, pk=pathology_id) pub=p.publication.all() return render_to_response('search/pathology.html', { 'pathology': p, 'pub': pub,}, context_instance=RequestContext(request))
I placed in template {% for publication in pub %}publication: {{ publication.title|safe }} <br /> {% endfor %}Query does not retrieve the publication.
IT WORKS!! Sorry, I needed to add the if pub to template. Thanks!!
Nice, happy to help :)
Tomh