I have the following models
class Book(models.Model):
name = models.CharField(max_length=140)
class UserProfile(models.Model):
favorites = models.ManyToManyField(Book, null=True, blank=True)
user = models.OneToOneField(User)
I need to craete a list of all books and show which ones are favorites and which ones are not.
I need a queryset for a view that gets me all the books like
Book.objects.all()
but i also need to know for each book if it is a favorite for that user and then pass this queryset to the template.
Thanks.
This is a relatively straightforward use of the ManyToManyField.
class Book(models.Model):
name = models.CharField(max_length=140)
class UserProfile(models.Model):
favorites = models.ManyToManyField(Book, null=True, blank=True)
user = models.OneToOneField(User)
favorite_books = this_user_profile.favorites.all()
for b in Book.objects.all():
if b in favorite_books:
print "Book", b.name, "is a favorite of this user!"
else:
print "Book", b.name, "is not a favorite of this user!"
ETA: Since you say you want to add it to the template, give it to the template as a list of tuples.
book_list = [(b, (b in favorite_books)) for b in Book.objects.all()]
In your template, have the code
{% for book, is_favorite in book_list %}
{% if is_favorite %}
{% comment %} Do something with this favorite book {% endcomment %}
{% else %}
{% comment %} Do something with this non favorite book {% endcomment %}
{% endif %}
{% endfor %}
Related
During work on my 1st app(kind of cookery book where it will be possible also to create meal plans) i have a problem to addapt one field from many-to-many(through) model to my html template. Field name is 'meal' in RecipeMealPlan model.
Here are my models:
class Recipe(models.Model):
title = models.CharField(max_length=50)
cooking_time = models.IntegerField(help_text='in minutes', validators=[MinValueValidator(1), MaxValueValidator(5000)])
difficulty_level = models.IntegerField(choices=DIFFICULTY_LEVELS, default=1)
description = models.TextField()
created = models.DateTimeField(auto_now_add=True)
cuisine = models.ForeignKey('Cuisine', on_delete=models.CASCADE, null=True)
ingredient = models.ManyToManyField(Ingredient, through='IngredientRecipe')
meal_plan = models.ManyToManyField('MealPlan', through='RecipeMealPlan')
class RecipeMealPlan(models.Model):
recipe = models.ForeignKey(Recipe, on_delete=models.CASCADE)
meal_plan = models.ForeignKey('MealPlan', on_delete=models.CASCADE)
meal = models.IntegerField(choices=MEALS)
MEALS = (
(1, 'Breakfast'),
(2, '2nd breakfast'),
(3, 'Lunch'),
(4, 'Snack'),
(5, 'Dinner')
)
class MealPlan(models.Model):
name = models.CharField(max_length=50)
amount = models.IntegerField(validators=[MinValueValidator(4), MaxValueValidator(6)])
Here is my view created to show mealplan details on my app:
class MealPlanDetailsView(View):
def get(self, request, id):
mealplan = MealPlan.objects.get(id=id)
recipes = mealplan.recipe_set.all()
return render(request, 'diet_app/mealplan_details.html', {'mealplan': mealplan, 'recipes': recipes})
And html template:
{% extends 'diet_app/base.html' %}
{% block title %}{{ mealplan|upper }}{% endblock %}
{% block content %}
<h2>{{ mealplan|upper }}</h2>
<ul> <p>Posiłki:</p>
{% for recipe in mealplan.recipemealplan_set.all %}
<li>{{ recipe.get_meal_display}}: {{ recipe }}</li>
{% endfor %}
</ul>
{% endblock %}
Everything looks fine but link to receipe details doestnt work:
<a href="/recipe/{{recipe.id}}/">
Link works if i write the loop like this:
{% for recipe in recipes %}
<li>{{ recipe.title }} </li>
{% endfor %}
But then i dont see meal name before recipe (meal name means Breakfast, dinner etc.). I don't how to write it down to see together meal name and recipe with link to recipe details.
I succeed only when i wrote those 2 loops combined but then i see my meal plan repeated few times.
Any ideas what should i do to make it work the way i want?
recipe.id is the id of the through model RecipeMealPlan, and not Recipe, so instead of recipe.id, you need to use recipe.recipe.id.
Also for sanity's sake, you could use something like recipemealplan instead of recipe as the variable name, so:
{% for recipemealplan in mealplan.recipemealplan_set.all %}
<li>{{ recipemealplan.get_meal_display}}: {{ recipemealplan }}</li>
{% endfor %}
I have a category model and list of posts related to those category also some post with same category name but when i wanted to make list of category section in template,
it showing duplicate name of category as it related to posts like:
food,
food,
desert,
style,
desert,
but I want like:
food,
desert,
style,
here is my code:
views.py
class ListCategory(ListView):
model = Post
paginate_by = 2
template_name = 'shit.html'
context_object_name = 'queryset'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
cate = Post.objects.all()
context['cate'] = cate
return context
models.py
class Category(models.Model):
title = models.CharField(max_length=20)
thumbnail = models.ImageField()
detail = models.TextField()
featured = models.BooleanField(default=True)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('post-category', kwargs={
'pk': self.pk
})
class Post(models.Model):
title = models.CharField(max_length=100)
overview = models.TextField()
featured = models.BooleanField(default=True)
timestamp = models.DateTimeField(auto_now_add=True)
user = models.ForeignKey(Author,on_delete=models.CASCADE)
thumbnail = models.ImageField()
category = models.ForeignKey(Category, on_delete=models.CASCADE)
tags = TaggableManager()
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('post-detail', kwargs={
'pk': self.pk
})
templates
{% extends "base.html" %}
{% load static %}
{% block content %}
<div class="sidebar-box ftco-animate">
<ul class="categories">
<h3 class="heading mb-4">Categories</h3>
{% for cat in cate %}
<li>{{cat.category}}<span>(12)</span></li>
{% endfor %}
</ul>
</div>
{% endblock content %}
Thank you so much!
Seems like you want to group your Posts, based on their category; so you can achieve that by iterating over the Category (instead of Post), and use the backward relationship to find out the related Post objects.
views.py
class ListCategory(ListView):
model = Category
paginate_by = 2
template_name = 'shit.html' # :)
context_object_name = 'queryset'
template:
{% extends "base.html" %}
{% load static %}
{% block content %}
<div class="sidebar-box ftco-animate">
<ul class="categories">
<h3 class="heading mb-4">Categories</h3>
{% for category in queryset %}
<li>{{category}}<span>{{ category.posts_set.count }}</span></li>
<ul>
{% for post in category.post_set.all %}
<li>{{ post }}</li>
{% endfor %}
</ul>
{% endfor %}
</ul>
</div>
{% endblock content %}
I also use {{ category.post_set.count }} instead of 12, since I think you are looking for the number of Post objects within each category.
You can use unique=True in desired field, to make every value unique. If you'll try to add new record with same value of unique field, a django.db.IntegrityError will be raised.
More about unique
More about model's fields options
I can't seem to figure this out. My view takes an argument for Team.id, and I want to return a context object with each User object with a certain value in User.profile.team and the associated date from Reports. I feel like I started on the right track, but am missing something. The output of my template contains all the data I'm trying to get, but not in a way that can be displayed logically.
Basically, I'm using models similar to the following:
class Reports(models.Model):
user = models.ForeignKey(User, null=True, on_delete=models.PROTECT)
product = models.CharField(max_length=15)
apps_activated = models.IntegerField(blank=True, null=True)
prem_submitted = models.DecimalField(max_digits=30, decimal_places=2)
class Team(models.Model):
name = models.CharField(max_length=255)
leader = models.ForeignKey(User,on_delete=models.PROTECT)
Extended user profile:
class Profile(models.Model):
COORDINATOR = 1
LEADER = 2
ADMIN = 3
ROLE_CHOICES = (
(COORDINATOR, 'Coordinator'),
(LEADER, 'Leader'),
(ADMIN, 'Admin'),
)
user = models.OneToOneField(User, on_delete=models.CASCADE)
team = models.ForeignKey(Team, on_delete=models.PROTECT,null=True)
role = models.PositiveSmallIntegerField(choices=ROLE_CHOICES, null=True, blank=True)
The closest I got in my view to return the desired data is with the following:
team = 1
team_name = Team.objects.get(id=team)
team_users = User.objects.filter(profile__team=team).all()
team_stats = []
for user in team_users:
team_stats.append(Reports.objects.filter(user_id=user.id))
With a template that looks like:
{% block content %}
<h1>{{ team }}</h1>
<ul>
{% for user in team_users %}
<li>{{ user.first_name }} {{ user.last_name }}</li>
{% endfor %}
</ul>
<ul>
{% for stat in team_stats %}
<li>
{% for line in stat %}
{{ line.product }} {{ line.type }} #etc #etc
{% endfor %}
</li>
{% endfor %}
</ul>
{% endblock %}
I thought I was on to something with prefetch_related(), but couldn't figure it out. Ideally, I'd only have to return one context object to my template.
Edit:
If it makes it more clear, this query returns the results I'm attempting to pass to the template:
select auth_user.first_name, auth_user.last_name, r.product, r.apps_activated, r.prem_submitted, r.conversion_percentage, r.type
from auth_user
join home_profile
on auth_user.id = home_profile.user_id
join reports_reports as r
on auth_user.id = r.user_id
where home_profile.team_id = 1
which returns rows that look like:
first_name-last_name-product-apps_activated-prem_submitted-conversion_rate-type
user_1-user_1_last-product_1-693-139764.00-53.86-type1
user_1-user_1_last-product_2-74-27400.10-0.00-type1
user_1-user_1_last-product_3-102-19782.00-47.00-type2
user_2-user_2_last-product_1-7-2437.70-0.00-type2
user_2-user_2_last-product_2-52-10608.00-42.54-type3
user_2-user_2_last-product_3-260.40-0.00-type3
Potential Solution 1:
So, I would do the following. Change user in your Reports model to link explicitly to Profile (which, in turn, links to the user)
class Reports(models.Model):
profile = models.ForeignKey(Profile, null=True, on_delete=models.PROTECT)
product = models.CharField(max_length=15)
apps_activated = models.IntegerField(blank=True, null=True)
prem_submitted = models.DecimalField(max_digits=30, decimal_places=2)
It seems likely that you are looking for a are probably looking for something like a select_related() query, adding in an extra ForeignKey field for the Profile:
reports = Reports.objects.select_related('profile')
You can check the resulting SQL via str(reports.query), which should result in sth along the lines of what you outlined in your question.
The returned cursor values are then translated into the appropriate ORM model instances, so that when you loop over these reports, you access the related tables' values via their own objects. However, these accesses along the pre-selected forward relations will not cause extra db hits:
{% for report in reports %}
{{ report.profile.user.username }}
{{ report.product }}
# ...
{% endfor %}
Let me know how you get on, and if we can brainstorm a solution more suitable, if this one isn't.
Potential Solution 2:
Perhaps another solution, and maybe the simplest of the two, would be to have a ManyToMany relationship in your Profile model to Reports:
class Profile(models.Model):
COORDINATOR = 1
LEADER = 2
ADMIN = 3
ROLE_CHOICES = (
(COORDINATOR, 'Coordinator'),
(LEADER, 'Leader'),
(ADMIN, 'Admin'),
)
user = models.OneToOneField(User, on_delete=models.CASCADE)
team = models.ForeignKey(Team, on_delete=models.PROTECT,null=True)
role = models.PositiveSmallIntegerField(choices=ROLE_CHOICES, null=True, blank=True)
reports = models.ManyToManyField(Reports, ...)
Then you should be able to loop over users in the template:
{% for user in users %}
{% for report in user.reports.all %}
{{ report.product }}
{% endfor %}
{% endfor %}
I'm building a commenting system, which is working fine but i'm now trying to integrate voting. So I made another model to handle that and I tried to pair it using ForeignKey. Not too familiar with ForeignKey but i've looked at some other answers here to see how to render it in the template. I tried that using the nested for loop in my template below but {{ j.user }} doesn't render anything. Can anyone tell me what I'm doing wrong?
models.py
class Comment(models.Model):
destination = models.CharField(default='1', max_length=12, blank=True)
author = models.CharField(max_length=120, blank=True)
comment_id = models.IntegerField(default=1)
parent_id = models.IntegerField(default=0)
comment_text = models.TextField(max_length=350, blank=True)
timestamp = models.DateTimeField(default=timezone.now, blank=True)
def __str__(self):
return self.comment_text
class CommentScore(models.Model):
user = models.ForeignKey(User)
comment = models.ForeignKey(Comment)
upvotes = models.IntegerField(default=0)
downvotes = models.IntegerField(default=0)
views.py
...
comment_list = Comment.objects.filter(destination=id)
score = CommentScore.objects.all()
context = {
'score': score,
'comment_list': comment_list,
}
return render(request, 'article.html', context)
template
{% for i in comment_list %}
<div class='comment_div'>
<h3>{{ i.author }}</h3>
{% for j in comment_list.score_set.all %}
{{ j.user }} #nothing comes up
{% endfor %}
<p>{{ i.comment_text }}</p>
</div>
{% endfor %}
when using _set, the reverse relationship lookup, you must provide the full model name, you must also specify which instance this list of related models this "set" is for so it should be
{% for j in i.commentscore_set.all %}
{% empty %}
No scores found
{% endfor %}
You may also wish to set a related_name for the foreign key
comment = models.ForeignKey(Comment, related_name='scores')
...
{% for j in i.scores.all %}
In my view.py, my context look like this:
context = {'books': books, 'asked_author': asked_author, 'role': role_field_list}
books is a list of dictionary (a list of book) and for each book, it exists one or more keys for which the key is named by a name in role_field_list:
I try to execute this template:
{% for book in books %}
{% for name in role %}
<h1>{{name}}</h1>
{% for authors in book.name %}
{{ authors.lastname }} {{authors.firstname}}
{% endfor %}
{% endfor %}
{% endfor %}
But book.name doesn't worked.
It takes name as a litteral 'name' not as a variable...
Thank you for any help.
To have more details about "field" and "type" of my context dictionnary, this my view.py:
def booksAuthor(request, author):
books_role = AuthorRoleBook.objects.filter(author_book=author).values() # get all the books written by one author
asked_author = AuthorBook.objects.get(pk=author) # get lastname and firstname for the author selected
books=[]
for book_role in books_role:
book = Book.objects.get(pk=book_role['book_id'])
book_dict = model_to_dict(book)
authors_role = AuthorRoleBook.objects.filter(book_id=book).values() # Get id of the different contributor for each book
role_field_list = ['auteur', 'traducteur', 'editeur_scientifique', 'directeur_publication']
for name in role_field_list:
list_author=[]
for author_role in authors_role:
if author_role['role_id']==name:
author=AuthorBook.objects.get(pk=author_role['author_book_id'])
author_dict = model_to_dict(author)
list_author.append(author_dict)
else:
pass
book_dict[name]=list_author
books.append(book_dict)
print(book_dict)
context = {'books': books, 'asked_author': asked_author, 'role': role_field_list}
return render(request, 'books_author.html', context)
And for my models, maybe it is not the easier way but this is:
class AuthorRoleBook(models.Model):
author_book = models.ForeignKey(AuthorBook)
role = models.ForeignKey('Role')
book = models.ForeignKey('Book')
class AuthorBook(models.Model):
lastname = models.CharField(max_length=100, blank=True, null=True)
firstname = models.CharField(max_length=100, blank=True, null=True)
.... ....
unique_together = (('lastname', 'firstname'),)
class Role(models.Model):
id = models.CharField(primary_key=True, max_length=100)
class Book(models.Model):
titre = models.TextField(blank=True, null=True)
....... .... ...
isbn_electronique = models.CharField(max_length=250, blank=True, null=True)
Thank you in advance.
For me it is working now. I keep the same views.py but in my template I do this:
{% for book in books %}
<h4>{{ book.titre }}</h4>
{% if book.directeur_publication %}
Directeur Publication:
{% for name in book.directeur_publication %}
{{ name.lastname }} {{name.firstname}}
{% endfor %}
{% endif %}
{% if book.editeur_scientifique %} ....
...
.....Etc
And for each value in role_field_list (['auteur', 'traducteur', 'editeur_scientifique', 'directeur_publication']) I put manually the value n my template in a "if condition".
It is not really a beautiful way... but it is working. For sure to use model object and not dictionary will be better.
Thanks for your answer, nevertheless if you have a smart code or idea to do that...