Display relationship attribute in django template - python

So, in my models I have:
class Ingredient(models.Model):
name = models.CharField(max_length=200)
articleNumber = models.IntegerField(unique=True)
costPerUnity = models.DecimalField(max_digits=4, decimal_places=2)
class Recipe(models.Model):
name = models.CharField(max_length=200)
ingredients = models.ManyToManyField(Ingredient, through='Recipe_Ingredient', related_name='recipes')
class Recipe_Ingredient(models.Model):
recipe = models.ForeignKey(Recipe, on_delete=models.CASCADE)
ingredient = models.ForeignKey(Ingredient, on_delete=models.CASCADE)
quantity = models.FloatField()
GRAM = 'g'
KILOGRAM = 'kg'
LITER = 'l'
CENTILITER = 'cl'
UNITY_CHOICES = (
(GRAM, 'Gram(s)'),
(KILOGRAM, 'Kilogram(s)'),
(LITER, 'Liter(s)'),
(CENTILITER, 'Centiliter(s)'),
)
quantityUnit = models.CharField(
max_length=2,
choices=UNITY_CHOICES,
default=GRAM,
)
In my template:
{% for ingredient in recipe.ingredients.all %}
<li>{{ ingredient.name }} - # quantity goes here </li>
{% endfor %}
How can I show the quantity atribute of the Recipe_Ingredient associated with this recipe and ingredient?
In the shell I could do this query: Recipe_Ingredient.objects.get(ingredient=Ingredient.objects.get(name='Cenoura'), recipe=Recipe.objects.get(name='Teste')), but I'm not quite sure how to do it in the template and what's the correct way of doing it.

You can accomplish what you want by iterating over the Receipe_Ingredient relationship.
{% for recipe_ingredient in recipe.recipe_ingredient_set.all %}
<li>{{ recipe_ingredient.ingredient.name }} - {{ recipe_ingredient.quantity }} </li>
{% endfor %}

Related

Django template - render object with specific attribute

I want my template to render object with specific attribute (products with specific category).
I am trying following code but it doesn't work.
May you please suggest how to write it correctly?
Template that I am trying make to work:
{% for instance in object_list %}
{% if instance.category == 'Statues' %}
{% endif %}
{% endfor %}
my models.py
class Category(models.Model):
categoryname = models.CharField(max_length=20)
description = models.CharField(max_length=200, blank=True, null=True)
#To make in name, not objXXX
def __str__(self):
return self.categoryname
class Product(models.Model):
name = models.CharField(max_length=20)
image = models.ImageField(upload_to='static/photos', default='http://placehold.it/700x400')
description = models.TextField(max_length=200, blank=True, null=True)
price = models.DecimalField(decimal_places=2, max_digits=10)
category = models.ForeignKey(Category, on_delete=models.PROTECT, blank=True, null=True)
#To make in name, not objXXX
def __str__(self):
return self.name
You need to use the field on the object;
{% for instance in object_list %}
{% if instance.category.categoryname == 'Statues' %}
{{ instance }}
{% endif %}
{% endfor %}
It won't work as a comparison against the string representation of the object.

How to check existence of a record in a model from current authenticated user in Django template?

Code below checks whether a user added a product in cart or not. If it is added to cart by this current user it should show remove from cart button else it should show a simple form to add product in cart.
{% for ordereditem in item.ordereditems_set.all %}
{% if ordereditem.quantity > 0 and ordereditem.user.username == user.username %}
Remove from cart
{% elif not ordereditem %} # here!
<!-- else if there is no record of 'ordereditem' from current user show this form to add it to cart-->
<form class="d-flex justify-content-left" method="POST" action="{{ item.get_add_to_cart_url }}">
{% csrf_token %}
<input type="number" name="number" value="1">
<input type="submit" value="Add to cart">
</form>
{% endif %}
{% endfor %}
the problem lies here {% elif not ordereditem %} it seems like my current if statement doesn't meet the condition I expect. I tried using {% else %} but it still shows the form even after adding product to cart.
This is how models look like:
class Item(models.Model):
title = models.CharField(max_length=100)
price = models.FloatField()
discount_price = models.DecimalField(max_digits=5,
decimal_places=2, verbose_name='Discount', null=True, blank=True)
image = models.ImageField(upload_to='products/%Y/%m/%d/')
image_cover = models.ImageField(upload_to='products/%Y/%m/%d/')
description = models.TextField()
slug = models.SlugField(max_length=150, blank=True, null=True)
category = models.CharField(max_length=15, choices=CATEGORY_CHOICE)
label = models.CharField(max_length=10, choices=LABEL_CHOICE)
associated_items = models.ManyToManyField("self", blank=True, null=True)
class OrderedItems(models.Model):
item = models.ForeignKey(Item, on_delete=models.CASCADE)
quantity = models.IntegerField(default=1)
user = models.ForeignKey(settings.AUTH_USER_MODEL,
on_delete=models.CASCADE)
ordered = models.BooleanField(default=False)
class Order(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL,
on_delete=models.CASCADE)
items = models.ManyToManyField(OrderedItems)
start_date = models.DateTimeField(auto_now_add=True)
ordered_date = models.DateTimeField()
ordered = models.BooleanField(default=False)
Here is the github link. Thank You
You need {% empty %} here:
{% for ordereditem in item.ordereditems_set.all %}
...
{% empty %}
No items
{% endfor %}

Django - Grouping 2 context datasets into 1 forloop

For each Product I need to show all it's reviews with an attached profilepicture, this requires queries into 3 models but I need a maximum of 2 for loops as when I do the Profile for loop all the images appear together as shown below. Ideally review & profile datasets are put together so only one profilepicture appears for each review.
Is there a way to fix this issue?
models.py
class Product(models.Model):
name = models.CharField(max_length=100)
brand = models.CharField(max_length=100)
cost = models.DecimalField(max_digits=8, decimal_places=2, default=0.00)
category = models.CharField(max_length=100)
releasedate = models.DateField()
description = models.TextField()
productphoto = models.ImageField(default='products/default_product.jpg', upload_to='products')
class Review(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE)
profile = models.ForeignKey(Profile, on_delete=models.CASCADE)
author = models.ForeignKey(User, on_delete=models.CASCADE)
rating = models.PositiveSmallIntegerField(default=1, validators = [MinValueValidator(1), MaxValueValidator(5)])
reviewtext = models.TextField()
class Profile(models.Model):
user = models.OneToOneField(User, on_delete = models.CASCADE)
profilephoto = models.ImageField(default='profiles/default_profile.jpg', upload_to='profiles')
views.py
class ProductDetailView(TemplateView):
# template_name = 'reviewApp/test.html'
template_name = 'reviewApp/product_detail.html'
def get_context_data(self, **kwargs):
prod = self.kwargs['pk']
context = super(ProductDetailView, self).get_context_data(**kwargs)
context['Products'] = Product.objects.filter(id=prod)
context['Reviews'] = Review.objects.filter(product=prod)
profile_ids = Review.objects.filter(product=prod).values_list('profile_id', flat=True)
context['Profiles'] = Profile.objects.filter(id__in=profile_ids)
return context
product.html
{% for prod in Products %}
<img src="{{prod.productphoto.url}}">
{{ prod.brand }} {{ prod.name }}
£{{ prod.cost }}
{{ prod.category }}
{{ prod.releasedate }}
{{ prod.description }}
{% endfor %}
{% for rev in Reviews %}
{% for prof in Profiles %}
<img src="{{prof.profilephoto.url }}">
{% endfor %}
{{ rev.rating }}
{{ rev.author }}
{{ rev.reviewtext }}
{% endfor %}
view
You can query the profile picture as rev.profile.profilephoto

Retrieve a filtered ForeignKey with Django

I have three main models, Picture, Place and PlaceRating:
class Picture(models.Model):
file = ImageField(max_length=500, upload_to="images")
user = models.ForeignKey(User, null=True, related_name="userpictures")
place = models.ForeignKey(Place, null=True, related_name='pictures')
class PlaceRating(models.Model):
place = models.ForeignKey(Place, null=True, related_name="placeratings")
user = models.ForeignKey(User, null=True, related_name="userratings")
rating = models.DecimalField(null=True, max_digits=4, decimal_places=1)
class Place(models.Model):
name = CharField(max_length=50)
I would like to display the place's rating given by the user, together with the place's image, but I cannot manage to do that as I would need to filter the ForeignKey and Django does not seem to allow that.
Example of what I would like to do:
View:
pictures = Picture.objects.filter(user=request.user)
Template:
{% for picture in pictures %}
<img src="{{ picture.file.url }}" class="bigpicture">
{{ picture.place.placeratings.0.rating|user:picture.user }}
{% endfor %}
For information, I managed to do it with templatetags, but this generates a lot of different queries to the database which I can't prefetch..:
{% for picture in pictures %}
<img src="{{ picture.file.url }}">
{% getplaceratingrelatedtopic picture.place.id picture.user.id %}
{% endfor %}
And:
#register.simple_tag
def getplaceratingrelatedtopic(placeid, userid):
print(placeid)
theplace = Place.objects.get(id=placeid)
user = User.objects.get(id=userid)
placerating = PlaceRating.objects.filter(author=user, place=place).last()
if rating:
return placerating.rating
else:
return ""
I work with Python 2.7/Django 1.9.
Any clue ? Thanks a lot!
You should move the logic to the model and use it in the template.
class Picture(models.Model):
file = ImageField(max_length=500, upload_to="images")
user = models.ForeignKey(User, null=True, related_name="userpictures")
place = models.ForeignKey(Place, null=True, related_name='pictures')
class PlaceRating(models.Model):
place = models.ForeignKey(Place, null=True, related_name="placeratings")
user = models.ForeignKey(User, null=True, related_name="userratings")
rating = models.DecimalField(null=True, max_digits=4, decimal_places=1)
class Place(models.Model):
name = CharField(max_length=50)
#property
def rating(self):
return self.placeratings.get().rating
{% for picture in picture_list %}
<img src="{{ picture.file.url }}" class="bigpicture">
{{ picture.place.rating }}: {{ picture.user.username }}
{% endfor %}

Indexing and searching related objects with haystack

I'm pretty new to search implementation, bear with me while I'm learning!
So my pet project is a recipe site and each recipe can have n steps. the model looks something like:
class Recipe(models.Model):
title = models.CharField(max_length=255)
description = models.TextField()
hotness = models.ForeignKey(Hotness)
recipe_diet = models.ManyToManyField(DietType)
ingredients = models.ManyToManyField(Ingredient, through="RecipeIngredient")
class DietType(models.Model):
diet_type = models.CharField(max_length=50)
description = models.TextField(null=True, blank=True)
class RecipeIngredient(models.Model):
recipe = models.ForeignKey(Recipe)
ingredient = models.ForeignKey(Ingredient)
quantifier = models.ForeignKey(Quantifier)
quantity = models.FloatField()
class RecipeSteps(models.Model):
step_number = models.IntegerField()
description = models.TextField()
recipe = models.ForeignKey(Recipe)
(shortened for brevity)
I want to index all of it: Recipe, RecipeIngredient, DietType and Steps...
The DietType and RecipeIngredient seem to be working fine, but the Steps are not. I assume this has to do with the usage of 'RelatedSearchQuerySet' ?
Here is my search_indexes.py:
from haystack import indexes
from recipes.models import Recipe
class RecipeIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
title = indexes.CharField(model_attr='title')
ingredients = indexes.MultiValueField(indexed=True, stored=True)
description = indexes.CharField(model_attr='description')
hotness = indexes.CharField(model_attr='hotness')
diet_type = indexes.MultiValueField(indexed=True, stored=True)
recipesteps = indexes.MultiValueField(indexed=True, stored=True)
def prepare_steps(self, object):
return [step.description for step in object.recipesteps.all()]
def get_model(self):
return Recipe
def load_all_queryset(self):
# Pull all objects related to the Note in search results.
return Recipe.objects.all().select_related()
Here is the template recipe_text.txt:
{{ object.title }}
{{ object.cuisine }}
{% for ingr in object.ingredients.all %}
{{ ingr.title }}
{% endfor %}
{{ object.description }}
{% for dt in object.recipe_diet.all %}
{{ dt.diet_type }}
{% endfor %}
{{ object.user }}
{{ object.hotness }}
{% for step in object.recipesteps.all %}
{{ step.description }}
{% endfor %}
{{ object.body }}
I can search ingredients, title, description, diet type - everything works, except the RecipeSteps.
Finally, I'm making queries through the shell only at the moment:
#producing results:
sq = SearchQuerySet().filter(content='onion') #ingredient
sq = SearchQuerySet().filter(content='bolognese') #title
sq = SearchQuerySet().filter(content='bologna') #description
#not producing any results:
sq = SearchQuerySet().filter(content='chop') #step
sq = RelatedSearchQuerySet().filter(content='chop').load_all() #assuming this does the expanded search
Any idea?
I have identified two issues:
The name prepare_steps in your RecipeIndex is wrong it should be prepare_{field_name} so change it to prepare_recipesteps
You are trying to access related steps object.recipesteps.all objects recipe_text.txt in a wrong way, it should be object.recipesteps_set.all. Or keep using recipesteps but add this as a related_name in RecipeSteps model for ForeignKey Recipe e.g.
class RecipeSteps(models.Model):
# //
recipe = models.ForeignKey(Recipe, related_name='recipesteps')

Categories