I am writing an application in Django that takes all of the athletes in an event, sorts them into categories (lightweight, heavyweight, etc), and then displays the athletes in each category sorted from the top scoring athlete to the bottom scoring athlete. I can't get my template to sort the athletes into classes; either it displays all of the athletes or none.
Here are the relevant sections of my models and template:
An athlete may be in multiple categories and each category has multiple athletes. Also the categories are ordered by date.
models.py
class Entry(models.Model):
athlete = models.ForeignKey(Athlete, related_name='entries')
event = models.ForeignKey(Event, related_name='entries')
athlete_category = models.ManyToManyField(Category, related_name='entries')
athlete_score = models.CharField(max_length=20, blank=True)
placing = models.SmallIntegerField(blank=True, null=True)
class Category(models.Model):
category_name = models.CharField(max_length=100)
class CategoryOrder(models.Model):
event = models.ForeignKey(Event)
category = models.ForeignKey(Classes)
category_order = models.SmallIntegerField()
event_placings.html
{% for category in categories %}
<p>{{ category.category_name }}</p>
{% for entry in entries %}
{% for athlete_category in entry.athlete_category %}
{% if athlete_category == category %}
<p>{{ entry.athlete.first_name }} {{ entry.athlete.last_name }} - {{ entry.placing }}</p>
{% endif %}
{% endfor %}
{% endfor %}
{% endfor %}
The template is supposed to list each of the categories and then all of the athletes in that category based on his placing. The output should be:
Men's Lightweight
John Doe - 1
Joe Public - 2
Women's Lightweight
Jane Doe - 1
Eva Braun - 2
etc.
Currently I get:
Men's Lightweight
Women's Lightweight
It lists the categories, but not the athletes. Where am I going wrong?
Looks like you have unnecessary loop in your template. It could be just like this:
{% for category in categories %}
<p>{{ category.category_name }}</p>
{% for entry in category.entries %}
<p>{{ entry.athlete.first_name }} {{ entry.athlete.last_name }} - {{ entry.placing }}</p>
{% endfor %}
{% endfor %}
To maintane ordering of athletes you can use Meta ordering on your Entry class or make method def athletes_ordered(self) on your Category class with something like
def athletes_ordered(self):
return self.entries.order_by('athlete_score')
In the second case you'll have to replace category.entries by category.athletes_ordered in the template.
Related
I reach the error in my django project when I was trying to display my view. The problem seems like related to the Course model but i honestly dont know where to begin with it.
My Teacher model:
class Teacher(models.Model):
GENDER_MALE = 0
GENDER_FEMALE = 1
GENDER_CHOICES = [(GENDER_MALE, 'Male'), (GENDER_FEMALE, 'Female')]
fname = models.TextField(max_length=20)
lname = models.TextField(max_length=20)
gender = models.IntegerField(choices=GENDER_CHOICES)
phone = models.IntegerField(default=None, blank=True, null=True)
email = models.TextField(max_length=30, default=None, blank=True, null=True)
faculty = models.ForeignKey('Faculty', on_delete=models.CASCADE )
def __str__(self):
return self.lname + ' ' + self.fname
My Course model:
class Course(models.Model):
name = models.TextField(max_length=50)
faculty = models.ForeignKey('Faculty', on_delete=models.CASCADE )
def __str__(self):
return f'{self.name}'
My Faculty model:
class Faculty(models.Model):
name = models.TextField(max_length=30)
def __str__(self):
return f'{self.name}'
My view:
def teacher(request, teacher_id):
teacher = get_object_or_404(Teacher, pk=teacher_id)
faculties = Faculty.objects.filter(teacher=teacher)
course = Course.objects.filter(teacher=teacher)
classrooms = Classroom.objects.filter(teacher=teacher)
students = Student.objects.filter(teacher=teacher)
# students = []
# for cls in classrooms:
# students.extend(Student.objects.filter(classroom=cls))
return render(request, 'polls/teacher.html', {'teacher': teacher,'faculties': faculties, 'courses':course,'classrooms':classrooms, 'students':students})
When i run the website, it point out in my view the code to be stoped at course = Course.objects.filter(teacher=teacher)
My template:
{% extends "polls/base.html" %}
{% block body %}
<h2>{{ teacher.fname }} {{ teacher.lname }}</h2>
<h3>Faculty</h3>
{% if faculties %}
{% for faculty in faculties %}
<p>{{ faculty.name }}</p>
{% endfor %}
{% else %}
<p> </p>
{% endif %}
<h3>Course</h3>
{%if courses %}
{% for course in courses %}
<p>{{ course.name }} </p>
{% endfor %}
{% else %}
<p> </p>
{% endif %}
<h3>Class</h3>
{% if classrooms %}
{% for class in classrooms %}
<p>{{ class.name }} </p>
{% endfor %}
{% else %}
<p> </p>
{% endif %}
<h3>Student</h3>
{% if students %}
{% for student in students %}
<p>{{ student.fname }} {{ student.lname }}</p>
{% endfor %}
{% else %}
<p> </p>
{% endif %}
{% endblock %}
This happens because of the following line:
course = Course.objects.filter(teacher=teacher)
The Course model has no field teacher, so you can't filter by it. To fix it there are two options:
Add a field to Course:
teacher = models.ForeignKey(Teacher, on_delete=models.CASCADE)
and then add a Teacher to every Course that already exists (or make it null=True).
Or you can filter Course by the Faculty that the teacher is a member of, to get all courses that have a teacher that belongs to that faculty:
courses = Course.objects.filter(faculty=teacher.faculty)
Or, using reverse foreign key lookup:
courses = teacher.faculty.course_set.all()
I have a model in Django
class Order(models.Model):
class Gender(models.IntegerChoices):
Male = (1,), _("Male")
Female = (2,), _("Female")
I want to send male and female in context
context["genders"] = Order.Gender
I use that in template like this
{% for gender in genders %}
<p>{{ gender }}</p>
{% endfor %}
I want to show male and female in front
Pass choices to the template, unpack and display them:
views.py
context["genders"] = Order.Gender.choices
template.html
{% for key, gender in genders %}
<p>{{ gender }}</p>
{% endfor %}
So I have these models:
excercises_choices = (('Bench Press', 'Bench press'),('Overhead Press', 'Overhead Press'), ('Squat', 'Squat'),
('Deadlift', 'Deadlift'))
unit_choices = (('kg','kg'), ('lbs', 'lbs'))
class Lifts(models.Model):
user = models.ForeignKey('auth.User', null=True)
excercises = models.CharField(max_length=200, choices=excercises_choices)
sets = models.IntegerField(null=True, blank=True)
reps = models.IntegerField(null=True, blank=True)
weight = models.FloatField()
unit = models.CharField(max_length=3, choices=unit_choices)
created_date = models.ForeignKey('Dates')
amrap_set = models.BooleanField(default=False)
amrap_rep = models.IntegerField(null=True, blank=True)
def __str__(self):
return self.excercises
class Dates(models.Model):
created_date = models.DateField(unique=True)
def __str__(self):
return str(self.created_date)
Let's say I have few lifts at different dates for admin and few lifts at different for xx user.
I want multiple lifts matching one date that's why I've made foreign key. (eg. 3 lifts to 2016-10-10 and 2 lifts to 2016-10-11).
Here is a view for showing it:
#login_required
def entries(request):
date = Dates.objects.all().order_by('-created_date')
lifts_by_user = Lifts.objects.filter(user=request.user)
return render(request, 'lift/entries.html', {'date': date,
'lifts_by_user': lifts_by_user})
And template:
{% extends 'lift/base.html' %}
{% block content %}
{{ user }}
{% if user.is_authenticated %}
{% for date in date %}
<p><strong>{{ date }}</strong>
{% for i in date.lifts_set.all %}
{{ i }}
{% endfor %}
add new lift</p>
{% endfor %}
{% endif %}
<p>
Delete lifts or dates
</p>
{% endblock %}
The problem is that I dont know how to separate it by dates AND by user.
This is how it looks like How do i keep this pattern date - lifts_to_that_date but for separate users? I dont want to see admin's entries while I am on test user
Have a look at the regroup template tag, it does exactly what you need.
You can do something like this in your view:
#login_required
def entries(request):
lifts_by_user = (Lifts.objects.filter(user=request.user)
.order_by('-created_date__created_date'))
return render(
request,
'lift/entries.html',
{'lifts_by_user': lifts_by_user}
)
And replace the for date in dates loop in your template with something like:
{% regroup lifts_by_user by created_date.created_date as lifts %}
<ul>
{% for day in lifts %}
<li>Date: {{ day.grouper }}
<ul>
{% for lift in day.list %}
<li>{{ lift }}</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
I've used a ul here so that it's easier to compare to the example in the docs, but obviously you can change the markup to whatever you need. It's important to know that regroup doesn't order its input, so you need to order by created_date in your view.
If you're using Django's dev version you can use this instead:
{% regroup lifts_by_user by created_date.created_date as lift_list %}
<ul>
{% for day, lifts in lift_list %}
<li>Date: {{ day }}
<ul>
{% for lift in lifts %}
<li>{{ lift }}</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
Which I think is a little clearer.
As an aside, none of this relies on having dates stored as a foreign key, but that's up to you.
Questions from comments:
order_by('-created_date__created_date') is joining Lifts to Dates through the Lifts.created_date foreign key and ordering by the Dates.created_date field. Have a look at https://docs.djangoproject.com/en/dev/topics/db/queries/#lookups-that-span-relationships for details.
for day, lifts in lift_list is using tuple unpacking.
As a quick example:
t = (1, 2, 3)
# first, second, third will have values 1, 2, 3 respectively
first, second, third = t
{% regroup lifts_by_user by created_date.created_date as lifts_list %} produces a list of namedtuples (again, only in the dev version, if you're using 1.10 or earlier it's a list of dicts so you can't use this trick) so as you're iterating through lift_list you can unpack the date and list of lifts into separate variables.
If you have a Lift instance called lift, you can get the pk for its date by using lift.created_date_id. Accessing it where you have the date URL in your example template is a little trickier because you have to get a lift out of the regrouped date's list. Something like this:
{% regroup lifts_by_user by created_date.created_date as lifts %}
<ul>
{% for day in lifts %}
<li>Date: {{ day.grouper }}
{# day.list.0 gets the first lift for this day #}
Date PK: {{ day.list.0.created_date_id }}
<ul>
{% for lift in day.list %}
<li>{{ lift }}</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
I have an order form where the user can choose one item and select a quantity. The price depends on how much is ordered. For example, each item is $10 if you order <100, but $7 if you order 100-200.
In the template, I want to display the pricing information underneath the form for each choice.
These are my Models:
class Product(models.Model):
name = models.TextField()
class Price(models.Model):
"""
This lets us define rules such as:
When ordering <100 items, the price is $10
When ordering 100-200 items, the price is $7
When ordering 200-300 items, the price is $5
etc
"""
price = models.FloatField()
min_quantity = models.PositiveIntegerField()
max_quantity = models.PositiveIntegerField()
product = models.ForeignKey(Product)
class Order(models.Model):
product = models.ForeignKey(Product, null=False, blank=False, default='')
quantity = models.IntegerField()
I can loop over the form fields and the queryset independently:
{% for choice in form.product.field.queryset %}
<h1>{{choice}} {{choice.price_set.all}}</h1>
{% endfor %}
{% for choice in form.product %}
<h1>{{ choice.tag }} {{ choice.choice_label }}</h1>
{% endfor %}
... but I don't know how to combine the loops to display the prices underneath the form fields.
Essentially, I want to select a reverse foreign key from a ModelChoicefield widget. I either need to loop over both the form fields and the queryset at once or access elements in the queryset from the form element. Ideally, this is what I'd like to do in my template:
{% for choice in form.product %}
<h1>{{ choice.tag }} {{ choice.choice_label }}</h1>
{% for price in choice.price_set.all %}
<h1>{{price}} etc...</h1>
{% endfor %}
{% endfor %}
Surely I'm not the first person with this use case. What's the best way to do this?
Edit: As requested, this is my form and my view. Reviewing this, I suppose I should have mentioned I was using the RadioSelect widget.
Form:
class OrderForm(forms.ModelForm):
class Meta:
exclude = ['date_added']
widgets = {
'mailer': forms.RadioSelect
}
model = Order
View:
def processOrder(request):
if request.method == 'POST':
orderForm = OrderForm(request.POST)
if orderForm.is_valid():
orderObject = orderForm.save()
return render(request, TEMPLATE_PREFIX + "confirm.html", {"orderObject": orderObject})
else:
return render(request, TEMPLATE_PREFIX + "register.html", { "form": orderForm })
else:
return render(request, TEMPLATE_PREFIX + "register.html", { "form": OrderForm()})
For (non)perfectionists with deadlines, this code works, albeit inefficiently.
{% for choice in form.product %}
{% for price_candidate in form.mailer.field.queryset %}
{% if price_candidate.id == choice.choice_value|add:0 %}
<h1>{{ choice.tag }} {{ choice.choice_label }}</h1>
{% for price in price_candidate.price_set.all %}
<h1>{{price}} etc...</h1>
{% endfor %}
{% endif %}
{% endfor %}
{% endfor %}
(The add:0 hack converts choice_value into an int. cf http://zurb.com/forrst/posts/Compare_string_and_integer_in_Django_templates-0Az )
I'm trying to run a search on a model that has a many to many field, and I want to filter the search using this field.
here is my current code:
search_indexes.py
class ListingInex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
business_name = indexes.CharField(model_attr='business_name')
category = indexes.MultiValueField(indexed=True, stored=True)
city = indexes.CharField(model_attr='city')
neighborhood= indexes.CharField(model_attr='neighborhood')
state = indexes.CharField(model_attr='state')
address = indexes.CharField(model_attr='address')
zip_code = indexes.CharField(model_attr='zip_code')
phone_number = indexes.CharField(model_attr='phone_number')
def get_model(self):
return listing
def index_queryset(self, using=None):
return self.get_model().objects.all()
def prepare_category(self, obj):
return [category.name for category in obj.category_set.all()]
listing_text.txt
{{ object.business_name }}
{{ object.state }}
{{object.city}}
{{object.zip_code}}
{{object.phone_number}}
{{object.neighborhood}}
{% for category in obj.category.all %}
{{ category.name }}
{% endfor %}
I'm trying to do a simple search like:
search_results = SearchQuerySet().filter(category=query_text).filter
it returns an empty list. Thanks in advance.
Reading your code I think the problem is here:
{% for category in obj.category.all %}
{{ category.name }}
{% endfor %}
Category is not an object, is a MultiValueField. Maybe if you try something like:
{% for category in object.category.all %}
{{ category.name }}
{% endfor %}
object.category.all instead of obj.category.all. I'm not sure the solution is this, but I'm pretty sure the error could be in this 3 lines.