25. Joins, filters and chaining commands
We’ve now built a page that shows the data present in the gene table. But we haven’t looked about data that arrives via foreign key.
These are setup as part of each query record and accessed via the appropriate variable as a linked query set, but we can then also iterate over.
Let’s jump back to our code and visit our gene.html template. We need to iterate over the foreign keys to extract the information we require.
{% with seq=gene.sequencing %} <tr><td>Sequencing Factory:</td> <td>{{seq.sequencing_factory}}</td></tr> <tr><td>Factory Location: </td> <td>{{seq.factory_location}}</td></tr> {% endwith %} {% with ec=gene.ec %} <tr><td>EC Name: </td><td>{{ec.ec_name}}</td></tr> {% endwith %} {% with links=gene.geneattributelink_set.all %} {% for link in links %} <tr><td>{{link.attribute.key}}</td><td>{{link.attribute.value}}</td></tr> {% endfor %} {% endwith %}
We can also filter our data. Let’s say we only want to view records which only have the chromosome entity in the gene table.
Let’s go back to the index.html page and add a new section:
Select gene location
As we have created a link to a URL with /list/ on it, we now need to add a new route to the urls.py file. So navigate to that file and update urlpatterns:
urlpatterns = [ path('', views.index, name='index'), path('gene/', views.gene, name='gene'), path('list/', views.list, name='list'), ]
This time list is capturing a string (with ).
We will also need to add a list function to views.py:
def list(request, type): genes = Gene.objects.filter(entity__exact=type) return render(request, 'genedata/list.html', {'genes': genes, 'type': type})
__exact is an inbuilt function which looks for an exact match to a field, in this case entity.
So we’re sending genes and type to a new template called list.html, which we need to create.
<html> <head> </head> <body> <h1>Filtered List: {{type}}</h1> <table> <tr><th>Gene ID</th></tr> {% for gene in genes %} <tr><td><a href="/gene/{{gene.pk}}">{{gene}}</a></td></tr> {% endfor %} </table> </body> </html>
Gene.objects.filter(entity__exact=type) is a filter (like a WHERE in SQL) and we can chain them together to make more sophisticated database queries.
Let’s say we want a list where the gene is chromosome and sense is positive.
Add a new link to index.html:
<h2>Show Positive Chromosome</h2> <a href="/poslist/">Show This List</a>
Add a new path in urlpatterns in urls.py:
path('poslist/', views.poslist, name='poslist'),
Add a new function in views.py:
def poslist(request): genes = Gene.objects.filter(entity__exact='Chromosome').filter(sense__startswith='+') return render(request, 'genedata/list.html', {'genes': genes, 'type':'PosList'})
You can see we have used .filter twice in the statement assigning the query set to genes here.
Further reading on how to use the Query Set API: https://docs.djangoproject.com/en/3.0/ref/models/querysets/
Wednesday 3 November 2021, 611 views
Next post: 26. Deleting and updating records Previous post: 24. Using the ORM in views.py
Advanced Web Development index
- 38. Writing API tests
- 37. Testing in Django
- 36. Class-based views in the Django REST framework
- 35. Building a RESTful web service in Django
- 34. Introduction to CRUD, REST and APIs
- 33. Refactoring with generic views in Django
- 32. Django validators
- 31. Django forms (2) – using the ModelForm class
- 30. Django forms (1)
- 29. JavaScript basics
- 28. Adding CSS to the template
- 27. Django templating
- 26. Deleting and updating records
- 25. Joins, filters and chaining commands
- 24. Using the ORM in views.py
- 23. Adding to the database by writing a script
- 22. Adding to the database with Django Admin
- 21. Migrations
- 20. ORM – work through example
- 19. An introduction to the Object-Relational Mapper
- 18. Altering the database
- 17. SQL functions and summaries
- 16. SQL Query performance
- 15. Queries and table joins in SQL
- 14. Inserts and queries in SQL
- 13. Good practice in relational database design
- 12. Limitations to database modelling
- 11. Building a database using SQL
- 10. Introduction to PostgreSQL
- 9. How to start writing a new application in Django
- 8. Building a lightweight project
- 7. Django URLs
- 6. Django templates
- 5. Django models
- 4. Django views
- 3. Creating a new hello app
- 2. Creating a new virtual environment
- 1. Setting up Django
Leave a Reply