Django how to allow usage of empty ranges in IntegerRangeField - python

I`m using IntegerRangeField (specific field for PostgreSQL) to represent year periods when a bancnote was issued.
Works fine when adding object to db and filtering with non-empty ranges such as 1990-2000, however there are few bancnotes, which were issued during only one year, so the period is for example 2005-2005. When setting range like this, this value becomes as 'None-None' after object adding to db.
Seems like IntegerRangeField doesn`t accept empty ranges.
I was trying to set range like '2005-(no value here)' with blank=True, null=True, this is fine for me, but in this case filters not working for this object.
To be more clear look at the example below:
2003-2008: displayed and filtered correctly
2003-2003: displayed as 'None-None', filtered wrongly
2003-(no value)/(no value)-2003: dislayed as '2003-None'/'None-2003'(satisfactorily for me), filters doesn`t work this
Also was thinking about using DateRangeField, but it provides day and month at the same time as input, what makes a mess. Are there any other possible and correct ways to do this?
Hope I was clear with my situation, looking forward for any advice. Please feel free to ask, I will provide any information. Thanks in advance!
Here is my models.py
from django.contrib.postgres.fields import IntegerRangeField
from django.core.validators import MinValueValidator, MaxValueValidator
from django.db import models
from datetime import datetime
class Bancnote(models.Model):
Dollar= 'Dollar'
Euro= 'Euro'
TYPE_CHOICES = (
(Dollar, 'Dollar'),
(Euro, 'Euro')
)
type = models.CharField(max_length=11, choices=TYPE_CHOICES, default=Dollar)
par = models.PositiveIntegerField()
year = IntegerRangeField(null=True, blank=True)
size = models.CharField(max_length=7)
sign = models.CharField(max_length=20)
desc = models.TextField(max_length=200)
image = models.ImageField(upload_to='bons_images')
def __str__(self):
return str(self.par) + ' ' + self.type + ' ' + str(self.year.lower) + '-' + str(self.year.upper)
filters.py
from django import forms
import django_filters
from .models import Bancnote
class BancnoteFilter(django_filters.FilterSet):
type = django_filters.ChoiceFilter(name='type', choices=Bancnote.TYPE_CHOICES,
widget=forms.RadioSelect(attrs={'class': 'radio'}), empty_label=None)
par_gt = django_filters.NumberFilter(name='par', lookup_expr='gte', widget=forms.Select)
par_lt = django_filters.NumberFilter(name='par', lookup_expr='lte', widget=forms.Select)
year = django_filters.NumericRangeFilter(name='year', lookup_expr='contained_by')
class Meta:
model = Bancnote
fields = ['type', 'par_gt', 'par_lt', 'year']
views.py
from django.shortcuts import render, render_to_response
from django.template import RequestContext
from django.shortcuts import render
from .models import Bancnote
from .filters import BancnoteFilter
def index(request):
bons_list = Bancnote.objects.all().order_by('par')
bons_filter = BancnoteFilter(request.GET, queryset=bons_list)
def image(request):
bons = Bancnote()
variables = RequestContext(request, {
'bons': bons
})
return render_to_response('catalogue/bon_detail.html', variables)
index.html
{% extends 'catalogue/base.html' %}
{% block title %}Catalogue{% endblock %}
{% load widget_tweaks %}
{% block sidebar %}
<form method="get">
<div class="well bs-sidebar" id="style" style="background-color:#fff">
<ul class="nav nav-pills nav-stacked">
<li>
<a href="#" class="toggle-menu" onclick="showcontent('#money-type')">Bancnote type
<i class="fa fa-chevron-up"></i>
</a>
</li>
<div id="money-type">
<ul class="nav nav-pills nav-stacked">
{% for type in filter.form.type %}
<li><a href="#">
{{ type.tag }}
<label for="{{ type.id_for_label }}">{{ type.choice_label }}</label>
</a></li>
{% endfor %}
</ul>
</div>
<li>
<a href="#" class="toggle-menu" onclick="showcontent('#par')">Bancnote par
<i class="fa fa-chevron-up"></i>
</a>
</li>
<div id="par">
{% render_field filter.form.par_gt id='from' %}{% render_field filter.form.par_lt id='to' %}
</div>
<li><a href="#" class="toggle-menu" onclick="showcontent('#period')">Issue years
<i class="fa fa-chevron-up"></i></a></li>
<div id="period">
<div class="range-input">
{% render_field filter.form.year maxlength='4' %}
</div>
<div class="range-slider">
<input value="1917" min="1917" max="2017" step="1" type="range">
<input value="2017" min="1917" max="2017" step="1" type="range">
</div>
</div>
</ul>
<button type="submit" class="btn btn-primary" style="text-align: center; width: 100%">
<span class="glyphicon glyphicon-search"></span> Search
</button>
</div>
</form>
{% endblock %}
{% block content %}
<div class="container-fluid">
<div class="row">
{# filtering here #}
{% for bon in filter.qs %}
{% if forloop.counter0|divisibleby:"4" %}
</div>
<div class="row">
{% endif %}
<div class="col-sm-3 col-lg-3">
<div style="display: block; text-align: center; margin: 0 auto">
<a href="{{ bon.id }}">
<img src="{{ bon.image.url }}" style="width: 50%; height: 50%"/>
<h5>{{ bon.par }} {{ bon.type }} {{ bon.year.lower}}-{{ bon.year.upper }}</h5>
</a>
</div>
</div>
{% endfor %}
</div>
</div>
{% endblock %}

The primary issue is that you're not storing ranges correctly. Per the django postgres docs,
All of the range fields translate to psycopg2 Range objects in python, but also accept tuples as input if no bounds information is necessary. The default is lower bound included, upper bound excluded; that is, [).
In short,
tuple(2005, None) is invalid, because postgres effectively treats an empty bound as infinity. Filtering fails in turn, because infinity exists outside of your bounds check.
tuple(2005, 2005) is invalid, because Django defaults to an excluded upper bound. Postgres normalizes [2005,2005) to empty, because the range effectively doesn't exist.
additionally, your ranges of tuple(2003, 2008) are not correct, since 2008 is not included. I'm pretty sure if you filtered for a range of 2008 to 2010, it would be excluded.
To represent a single year, you want to use either:
tuple(2005, 2006)
Range(2005, 2005, bounds='[]') Note that bounds is a string literal (psycopg2 docs).
In regards to filtering, the filter performs a startwsith and endswith check, which I believe is inclusive. So, filtering your year by 2005 to 2005 should yield the expected value from the DB. The filter should function correctly once the ranges are stored correctly.
Postgres range docs for reference. Section 8.17.5 contains info on boundary behavior and normalization.
Example Range:
from psycopg2.extras import NumericRange
NumericRange(2005, 2008, bounds='[]')

Related

Transferring user input from one page to another

I am making a website that allows students to find upcoming study sessions for their courses. I am doing this in Django and HTML. A student uploads their courses to the site and they are shown on the courses page as buttons (ex. CS 101 - Intro to CS). When a student clicks on one of their courses (button), it is supposed to bring them to a page that shows available study sessions for that course. I am stuck because I do not know how to properly filter the available study sessions on the next page based on which course is clicked. Is there a way to store the info of the course as a variable so when the button is clicked I can use that variable to filter the results? EDIT: I have made these changes and now I am getting a ValueError too many values to unpack expected 2. I am almost certain it is happening in my views.
Here is the page that shows a user's courses:
<div class="container h-100" style="top:50%; bottom:50%; width:100%;">
<div class="row">
{% if courses_list %}
{% for course in courses_list %}
<a type="button" class="btn btn-outline-secondary" href="{% url 'study:course-session'%}" >{{ course.subject }} {{ course.number}}-{{course.name}} </a>
<br><br><br>
{% endfor %}
{% else %}
<p class="text-center">You have not added any courses yet!</p>
{% endif %}
</div>
</div>
And here is the page that I am trying to filter the list of study sessions (which have a field course that is a ForeignKey to the Courses model):
<h1><center>Upcoming Study Sessions</center></h1>
<div>
<a class="btn btn-success" style="position:absolute; margin-right:2px; top:15%; right:0;" href="{% url 'study:courses' %}" role="button" >Back to My Courses</a>
</div>
<br><br>
<div class="container h-100" style="top:50%; bottom:50%; width:100%;">
<div class="row">
<button type="button" class="btn btn-outline-secondary" >Date/Time: {{ session.date }} <br> Location: {{ session.location }} </button>
<br><br><br>
</div>
</div>
View for the template:
def CourseSessionView(request, course_pk):
course_wanted = Course.objects.get(id=course_pk)
try:
return Study.objects.filter(course=course_wanted)
except:
return messages.error(request, 'There are no upcoming study sessions at this time for the requested course.')
Model for course and session:
class Course(models.Model):
SUBJECT_CHOICES = [
('AAS', 'AAS')
]
subject = models.CharField(
max_length=4, choices=SUBJECT_CHOICES, default='')
number = models.PositiveSmallIntegerField(
validators=[MaxValueValidator(9999)], default=0)
name = models.CharField(max_length=100, default='')
roster = models.ManyToManyField(
Student, blank=True, related_name="courses")
# Use [Student object].courses.all() to see all of a student's courses
def __str__(self):
return f"{self.subject} {self.number} - {self.name}"
class Study(models.Model):
organizer = models.ForeignKey(Student, on_delete=models.CASCADE)
date = models.DateTimeField()
# Use [Student object].studies.all() to see all of a student's study sessions
attendees = models.ManyToManyField(Student, related_name="studies")
location = models.CharField(max_length=30)
course = models.ForeignKey(Course, on_delete=models.CASCADE)
def __str__(self):
return f"{self.date} - {self.location}"
Url:
path('<int:course_pk>/sessions/',
views.CourseSessionView, name='course-session')
Note: The function based views' name doesn't require to be in PascalCase as in your case, it should be in snake_case.
The page that show the user's courses, there you need to pk of courses:
<div class="container h-100" style="top:50%; bottom:50%; width:100%;">
<div class="row">
{% if courses_list %}
{% for course in courses_list %}
<a type="button" class="btn btn-outline-secondary" href="{% url 'study:course-session' course.pk %}" >{{ course.subject }} {{ course.number}}-{{course.name}} </a>
<br><br><br>
{% endfor %}
{% else %}
<p class="text-center">You have not added any courses yet!</p>
{% endif %}
</div>
</div>
Your view for the template, i am defining it in snake_case, since its recommended way.
def course_session(request, course_pk):
course_wanted = Course.objects.get(id=course_pk)
study_courses=''
try:
study_courses= Study.objects.filter(course=course_wanted)
except:
messages.error(request, 'There are no upcoming study sessions at this time for the requested course.')
else:
return render(request,'anyfolder/anyfile.html',{'study_courses':study_courses})
return render(request,'anyfolder/anyfile.html') #then it will show only your error message.
Your url in urls.py be like:
path('any_route_name/<int:course_pk>/', views.course_session, name='course_session')
Note: Never forget to pass / at the end of your url or route_name.
Then, in your any template file you can access it and run loop:
{% for study in study_courses %}
{{study.organizer}},{{study.date}}
{% endfor %}
Then, you can access all its properties, and take benefit of ManyToOne relation.
This is going to be a very general type of answer since you are not providing your models or your views, but I think the idea would be the following.
First, in your template you can pass a parameter for the course number in the url:
your_template.html
<a class="btn btn-outline-secondary"
href="{% url 'study:course-session' course.pk %}">
{{ course.subject }} {{ course.number}}-{{course.name}}
</a>
Then in your view you can access that value, and from it get the course:
views.py
def the_view_name(request, course_pk):
# Here you now have access to the course's primary key, pk, so you can get the
# course and filter the study sessions by that course, etc...
You will need to modify the urls.py so the view can accept this new parameter:
urls.py
path('the_view_name/<int:course_pk>', views.the_view_name, name='the_view_name'),
EDIT
Make the following changes:
First to your views.py:
def CourseSessionView(request, course_pk):
try:
course_wanted = Course.objects.get(id=course_pk)
except:
return messages.error(request, 'course not found')
study_sessions = Study.objects.filter(course=course_wanted)
if study_sessions.count() < 1:
return messages.error(request, 'There are no upcoming study sessions at this time for the requested course')
context = {
'study_sessions': study_sessions,
}
return render(request, 'study/your_template_file.html', context)
Then in your html
<h1><center>Upcoming Study Sessions</center></h1>
<div>
<a class="btn btn-success" style="position:absolute; margin-right:2px; top:15%; right:0;" href="{% url 'study:courses' %}" role="button" >Back to My Courses</a>
</div>
<br><br>
<div class="container h-100" style="top:50%; bottom:50%; width:100%;">
{% for session in study_sessions %}
<div class="row">
<button type="button" class="btn btn-outline-secondary" >Date/Time: {{ session.date }} <br> Location: {{ session.location }} </button>
<br><br><br>
</div>
{% endfor %}
</div>

how show favorite list in django

I have a list of favorites and I want to show them when I click on the interest button after I click on the list and my heart will be bold. The second part, ie filling the heart, is done correctly, but when I want to show the list, it does not show anything and gives the following error.
Reverse for 'show_Book' not found. 'show_Book' is not a valid view function or pattern name.
model.py
class Book (models.Model):
BookID= models.AutoField(primary_key=True)
Titel=models.CharField(max_length=150 )
Author=models.ForeignKey('Author',on_delete=models.CASCADE)
Publisher=models.ForeignKey('Publisher',on_delete=models.CASCADE) translator=models.ForeignKey('Translator',null='true',blank='true',on_delete=models.SET_NULL)
favorit=models.ManyToManyField('orderapp.Customer',related_name='favorit', blank=True)
view.py
def show_Book(request,BookID):
showBook=get_object_or_404(Book,BookID=BookID)
is_favorite=False
if showBook.favorit.filter(id=request.user.id).exists():
is_favorite=True
return render (request , 'showBook.html', {'is_favorite':is_favorite,})
def favoritbook (request, BookID):
showBook=get_object_or_404(Book,BookID=BookID)
if showBook.favorit.filter(id=request.user.id).exists():
showBook.favorit.remove(request.user)
else:
showBook.favorit.add(request.user)
return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
def favoritlist(request):
user=request.user
favoritbooks=user.favorit.all()
context={'favoritbooks':favoritbooks}
return render (request,'favotritlist.html',context)
url.py
path('showbookstor/<int:id>', views.show_BookStor, name='show_BookStor'),
path('books/favorit/<int:BookID>/', views.favoritbook, name='favoritbook'),
path('books/favorit/', views.favoritlist, name='favoritlist'),
showbook.html
{% if is_favorite %}
<li id="sell">add to favorit <i class="fas fa-heart"></i> </li>
{% else %}
<li id="sell"> delete favorit <i class="far fa-heart"></i> </li>
{% endif %}
favoritlist.html
{% for Book in favoritbooks %}
<section id="card" class="col-md-6 col-lg-3 col-sm-6 col-xs-12">
<img src= {{Book.Image.url}}>
<h1 id="bookname">{{Book.Titel}}</h1>
<p>{{Book.Author}}</p>
</section>
{% endfor %}
you use {% url 'show_Book' Book.BookID %} but you don't have any definition in the urls.py with name show_Book try changing
path('showbookstor/<int:id>', views.show_BookStor, name='show_BookStor')
to
path('showbook/<int:id>', views.show_Book, name='show_Book')

Django not passing context well

I was trying to pass a query set using context. But on template page the context is not working.
As I am implementing two queries in same view one query set is working fine but the other query is not passed well. Here is my view
# Create your views here.
def xray_result_view(request):
q=query_holding_together.objects.filter(which_user=request.user)
for x in q:
all_reports=xray_result.objects.get(which_query=x)
print(all_reports.sys_gen_result)
return render(request,'XRay/result.html',{'reports':all_reports})
when q is passed as template it is working as it should but it is not working for all reports. here is my template
{% extends "login/successful.html" %}
{% block middle_window %}
</div>
<div class="container adjust-ment">
<div class="row">
<div class="col-12">
Previous X-ray Results
</div>
</div>
<div class="row">
<div class="col-12">
Result
</div>
</div>
<div class="row">
<div class="col-12">
{% for y in reports.iterator %}
File Name:<br>
Date and Time of Upload:<br>
System Generated Result:{{ y.sys_gen_result }}<br>
Doctor's Comment on Result:{{ y.doctor_comment }}<br>
{% endfor %}
</div>
</div>
</div>
{%endblock middle_window %}
You are not passing a queryset in template, instead you are sending an object. Let me explain:
for x in q:
all_reports=xray_result.objects.get(which_query=x) #<-- Here
Here all_reports is a variable which has only a xray_result object. after the iteration is complete, all_reports will contain only the last object from q.
Instead, you can try like this:
def xray_result_view(request):
all_reports=xray_result.objects.get(which_query__which_user=request.user)
return render(request,'XRay/result.html',{'reports':all_reports})
And update the template:
{% for y in reports %}
File Name:<br>
Date and Time of Upload:<br>
System Generated Result:{{ y.sys_gen_result }}<br>
Doctor's Comment on Result:{{ y.doctor_comment }}<br>
{% endfor %}
Finally, consider using PascalCase when writing Class names(as per pep8 standard).
As #ruddra pointed out that there was problem with loop.
so for that I tried below workaround and it worked as a charm.
def xray_result_view(request):
q=query_holding_together.objects.filter(which_user=request.user)
all_reports=xray_result.objects.none()
for x in q:
all_reports = all_reports | xray_result.objects.filter(which_query=x)
for x in all_reports:
print(x.sys_gen_result)
return render(request,'XRay/result.html',{'reports':all_reports})

Django for loop in html template not displaying

So I am trying to get the latest post and I ran into a problem where it will not display the post
views.py
def latest_post(request):
latest = Post.objects.all().order_by('-id')[:1]
context = {'latestPost': latest}
return render(request, 'latest_post.html', context)
Note: I also tried it with this, latest = Post.objects.all()
There are entries in the database, I tested with the shell
from blog.models import Post
>>> Post.objects.all()
<QuerySet [<Post: test>, <Post: okay so>, <Post: okay-okay>]>
latest_post.html
{% for newest in latestPost %}
<section class="hero is-primary is-medium">
<div class="hero-body header">
<div class="container">
<div class="font">
<h1 class="title is-1">
<span id="blog_title">{{ newest.title }}</span>
</h1>
<h2 class="subtitle is-3 subup">
<span id="blog-subtitle">{{ newest.content|truncatechars:20|safe }}</span>
</h2>
<h2 class="subtitle is-5 dateup">
<span id="blogdate">{{ newest.timestamp }}</span><br><br>
Read More >>
</h2>
</div>
</div>
</div>
</section>
{% endfor %}
in my blog_index.html I have the following
{% extends "blog_base.html" %}
{% block blog_main %}
{% load staticfiles %}
{% include 'latest_post.html' %}
<p> other html here </p>
{% endblock %}
Latest_post.html displays when i use {% include 'latest_post.html' %} only if I don't use
{% for newest in latestPost %}
{% endfor %}
So i am sure there aren't any typos somewhere that prevents the latest_post.html from displaying in my index page.
my models.py
class Post(models.Model):
title = models.CharField(max_length=120)
slug = models.SlugField(unique=True)
content = models.TextField()
draft = models.BooleanField(default=False)
publish = models.DateField(auto_now=False, auto_now_add=False)
updated = models.DateTimeField(auto_now=True, auto_now_add=False)
timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)
def __str__(self):
return self.title
def blog_url(self):
return reverse("blogposts:blogdetail", kwargs={"slug": self.slug})
Additional Notes: python3, django 1.11, sqlite.
Also, No errors are displayed in the console. any help would be appreciated! thank you!!
Looks like you are passing context variable to latest_post.html directly:
return render(request, 'latest_post.html', context)
But there is no such context variable latestPost in blog_index.html.
What you need to do is add context to blog_index.html. Add this to index view also:
latest = Post.objects.all().order_by('-id')[:1]
context = {'latestPost': latest}
return render(request, 'blog_index.html', context)
Also you can use first to select first element in queryset.
In your previous code you are limiting it by using [:1] so it only returns one item. Then you are using a forloop again on one item. Not the best way to do things.
Are you trying to get only one item from the post? If yes, Forloop is not needed change your latest_post.html to
<section class="hero is-primary is-medium">
<div class="hero-body header">
<div class="container">
<div class="font">
<h1 class="title is-1">
<span id="blog_title">{{ latestPost.title }}</span>
</h1>
<h2 class="subtitle is-3 subup">
<span id="blog-subtitle">{{ latestPost.content|truncatechars:20|safe }}</span>
</h2>
<h2 class="subtitle is-5 dateup">
<span id="blogdate">{{ latestPost.timestamp }}</span><br><br>
Read More >>
</h2>
</div>
</div>
</div>
</section>
You don't even need to use the [:1] in your views. There are better ways to do this
I also stuck in the same problem and I was damn confirmed that I was doing any silly mistakes.
Unfortunately, I found one. So,
Try this:
context = {'latest': latest}
Instead of:
context = {'latestPost': latest}
I hope it works.

Django template for loop iteration by distinct foreign key

In my app, I have 3 models: Issue, Series, and Character. The Issue model has a Series ForeignKey, and a Character ManyToManyField. Here they are simplified:
class Character(models.Model):
name = models.CharField('Character name', max_length=200)
desc = models.TextField('Description', max_length=500)
class Series(models.Model):
name = models.CharField('Series name', max_length=200)
desc = models.TextField('Description', max_length=500)
class Issue(models.Model):
series = models.ForeignKey(Series, on_delete=models.CASCADE, blank=True)
name = models.CharField('Issue name', max_length=200)
number = models.PositiveSmallIntegerField('Issue number')
date = models.DateField('Cover date')
desc = models.TextField('Description', max_length=500)
characters = models.ManyToManyField(Character, blank=True)
cover = models.FilePathField('Cover file path', path="media/images/covers")
I have a Character template that displays information about the character. I want to also display Issues the Character is in, sorted by Series.
{% extends "app/base.html" %}
{% block page-title %}{{ character.name }}{% endblock page-title %}
{% block content %}
<div class="description">
<p>{{ character.desc }}</p>
</div>
<div class="issues">
<h3>Issues</h3>
{% for series in character.issue_set.all %}
<div>
{{ series.name }}
<ul>
{% for issue in character.issue_set.all %}
{% if issue.series.name == series.name %}
<li>
<img src="/{{ issue.cover }}" alt = "{{ series.name }}" >
<p>Issue #{{ issue.number }}</p>
</li>
{% endif %}
{% endfor %}
</ul>
</div>
{% endfor %}
</div>
{% endblock content %}
Obviously, the way this currently formats is that for every issue in the set, it outputs the series title, and then each issue in the set.
<div class="issues">
<h3>Issues</h3>
<div>
Series 1
<ul>
<li>
<img src="/media/images/covers/01.jpg" alt="Series 1">
<p>Issue #1</p>
</li>
<li>
<img src="/media/images/covers/02.jpg" alt="Series 1">
<p>Issue #2</p>
</li>
</ul>
</div>
<div>
Series 1
<ul>
<li>
<img src="/media/images/covers/01.jpg" alt="Series 1">
<p>Issue #1</p>
</li>
<li>
<img src="/media/images/covers/02.jpg" alt="Series 1">
<p>Issue #2</p>
</li>
</ul>
</div>
</div>
Here's what I would like to see:
<div class="issues">
<h3>Issues</h3>
<div>
Series 1
<ul>
<li>
<img src="/media/images/covers/01.jpg" alt="Series 1">
<p>Issue #1</p>
</li>
<li>
<img src="/media/images/covers/02.jpg" alt="Series 1">
<p>Issue #2</p>
</li>
</ul>
</div>
</div>
I've researched quite a bit on templating, and I'm not seeing a way to get a listing based on distinct values. I've also tried creating a new set in my Character or Issue model that I could use to replace issue_set.all, but I have yet to get it working.
EDIT: Upon request of marcusshep, the Character view is using the generic DetailView:
class CharacterView(generic.DetailView):
model = Character
template_name = 'app/character.html'
I would use a function based view rather than a class based generic view. Reason being that your required behavior is going beyond something generic.
In your function you can build the queryset you desire instead of having to fight with the one provided by generic.DetailView.
def my_view(request, *args, **kwargs):
character = Character.objects.get(id=request.GET.get("id", None))
issues = character.issue_set.all().order_by("series__name")
return render(request, 'app/character.html', {"issues": issues})
Alternatively, you can use what you already have and override the DetailView's get_queryset() method.
class CharacterView(generic.DetailView):
model = Character
template_name = 'app/character.html'
def get_queryset():
# return correct queryset
The biggest problem though is that there will be more aspects that will need to use this set. For instance, I'll be adding Creators, Story Arcs, etc. they will have their own pages and will need to display related issues, sorted by series as well. It would be nice to have a solution that can be used by any of these templates without much code re-use.
This is a very common problem in all areas of programming. A very simple way to solve this would be to isolate the logic in one function and call that function whenever you need it.
def my_issues_query():
# find the objects you need
def my_view(request, *args, **kwargs):
issues = my_issues_query()
You can also take advantage of pythons decorator functions. (Which is my favorite approach.)
def has_issues(view_function):
def get_issues(request, *args, **kwargs):
# find all the issues you need here
# you'll only need to write this logic once.
issues = Issues.objects.filter(...)
return issues
return get_issues
#has_issues
def my_view(request, *args, **kwargs):
# this functions namespace now contains
# the variable `issues`.
# which allows for the use of the query ie.
return render(
request,
"my_templates/template.html",
{"issues":issue}
)

Categories

Resources