django queryet to print product and it's models - python

my models are
class Product(models.Model):
name = models.CharField(max_length=50)
desc = models.CharField(max_length=50)
def __unicode__(self):
return self.name
class ProductModels(models.Model):
product = models.ForeignKey(Product)
name = models.CharField(max_length=50)
price = IntegerField(max_length=50)
def __unicode__(self):
return self.name
i can easily print in python , product and it's releated models , but in django template , i am unable to figure out how to print them .
I want data to be seen in html pages like this :
product1 modelpm1
modelpm2
product2 modelpm3
modelpm4
modelpm5
and so on .....
Of course I have created table and all the html related tags properly, but I am unable to figure out how to print this way in template.

Its the same in the template:
<ul>
{% for product in objects %}
<li>{{ product }}
<ul>
{% for product_model in product.productmodel_set.all %}
<li>{{ product_model }}</li>
{% endfor %}
</ul></li>
{% endfor %}
</ul>
Use the following view:
def product_list(request):
return render(request, 'template.html', {'objects': Product.objects.all()})
Or, if you prefer, use the generic views:
class ProductList(ListView):
template = 'template.html'
queryset = Product.objects.all()

Related

Filtering through relationship in views or template

I have one simple model called Product
class Product(models.Model):
title = models.CharField(max_length=150)
def __str__(self):
return self.title
And one other model called ExternalProduct
class ExternalProduct(models.Model):
title = models.CharField(max_length=100)
external_id = models.CharField(max_length=25)
internal_product = models.ForeignKey(
Product,
on_delete=models.CASCADE,
blank=True,
related_name='external_products',
)
price = models.IntegerField()
brand = models.ForeignKey(Brand, on_delete=models.CASCADE, blank=True)
image_url = models.CharField(max_length=255)
product_url = models.CharField(max_length=255)
store = models.CharField(max_length=50)
active = models.BooleanField(default=True)
class Meta:
ordering = ['price']
def __str__(self):
return self.title
On the detailed view of the Product, I want to display the price of all ExternalProduct related to the Product. It works all fine with this view
# products/views.py
class ProductDetailView(DetailView):
model = Product
context_object_name = 'product'
template_name = 'products/product_detail.html'
and this template
# product_detail.html
{% extends '_base.html' %}
{% block title %}{{ product.title }}{% endblock title %}
{% block content %}
<div class="book-detail">
<h2>{{ product.get_active_external_products }}</h2>
</div>
<div>
<ul>
{% for ep in product.external_products.all %}
<li>{{ ep.price }}</li>
{% endfor %}
</ul>
</div>
{% endblock content %}
The problem is that I just want to display the price of ExternalProduct which has active=True
Been trying to solve it with a custom method in products views.py
class ProductDetailView(DetailView):
model = Product
context_object_name = 'product'
template_name = 'products/product_detail.html'
def get_active_external_products(self):
return self.external_products.objects.filter(active=True)
And a modified for loop inside product_detail.html
{% for ep in product.get_active_external_products %}
<li>{{ ep.price }}</li>
{% endfor %}
But unfortunately without any success. The site does not crash, just doesn't show anything other than the rest of the html file.
Any advice?
You can use prefetch_related with Prefetch to filter only active external products. For this you need to override get_queryset() method in your view:
from django.db.models import Prefetch
class ProductDetailView(DetailView):
model = Product
context_object_name = 'product'
template_name = 'products/product_detail.html'
def get_queryset(self):
return Product.objects.prefetch_related(Prefetch("external_products", queryset=ExternalProduct.objects.filter(active=True), to_attr="active_external_products"))
Note to_attr="active_external_products" part. It tells that active external products will be available as active_external_products attribute. So in template you can get them like this:
{% for ep in product.active_external_products %}
<li>{{ ep.price }}</li>
{% endfor %}
Or you can just override get_context_data() to insert external products to the context directly:
class ProductDetailView(DetailView):
model = Product
context_object_name = 'product'
template_name = 'products/product_detail.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(****kwargs)
context["active_external_products"] = self.object.external_products.filter(active=True)
return context
In this case in template you can use active_external_products variable name directly:
{% for ep in active_external_products %}
<li>{{ ep.price }}</li>
{% endfor %}

How to access child class object that inherits parent class?

I have parent class and child class, that inherits parent class. And that is okay, I can iterate with for loop. Now I want to access child class (example: 'product_type' So basically, I'm confused how we inherits stuff from child class inside the same loop...
views.py
from django.views import generic
from . models import Category
from django.shortcuts import render
class CategoryListView(generic.ListView):
model = Category
template_name = 'category_list.html'
models.py
from django.db import models
import uuid
class Category(models.Model):
name = models.CharField(max_length=100, help_text='Category name')
def __str__(self):
return self.name
class Meta:
verbose_name_plural = 'Categories'
class Product(models.Model):
product_name = models.CharField(max_length=255, help_text='Product name')
# product_spec = models.TextField(max_length=5000, help_text='Product specs')
product_type = models.ForeignKey('Category', on_delete=models.SET_NULL, null=True)
def __str__(self):
return self.product_name
category_list.html
{% extends 'base.html' %}
{% block body %}
{% for page in category_list %}
<li>{{ page.name }}</li>
<li>{{ page.product_name }} # <--------------- Now this is the point of
#my problem, I want to get
#product name from child
#class
#this returns empty <li>
{% endfor %}
{% endblock %}
you can to this
{% extends 'base.html' %}
{% block body %}
{% for page in category_list %}
<li>{{ page.name }}</li>
<li>{{ page.product_set.first.product_name }}
product name from child class, this returns empty <li>
{% endfor %}
{% endblock %}
First off change your product_type name to just category its way easier to understand and add an attribute related_name to it like this:
class Product(models.Model):
name = models.CharField(max_length=255, help_text='Product name')
category = models.ForeignKey('Category', on_delete=models.SET_NULL, null=True, related_name='products')
then in your template
{% for category in category_list %}
{{ category.name }}
{% for product in category.products.all %}
{{ product.name}}
... other product data
{% endfor %}
{% endfior %}
product_typein Product is a ForaignKey which means their will be multiple products having same Category so their exist two solutions
make product_type in Product one to one key, with this you shuld get single name by {{ page.product.product_name }}
print list of all products of the category, you can do this by iterating page.product_set as it is a list (iterable) with for loop.

How to Access Many to many field in class based views in Django?

I have two models, Author & Book in models.py
class Author(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=40)
email = models.EmailField()
age = models.IntegerField()
def __str__(self):
return '%s %s' %(self.first_name, self.last_name)
def __unicode__(self):
return '%s %s' %(self.first_name, self.last_name)
class Book(models.Model):
title = models.CharField(max_length=100) #name = title
pages = models.IntegerField()
price = models.DecimalField(max_digits=10, decimal_places=2)
rating = models.FloatField()
author = models.ManyToManyField(Author)
publisher = models.ForeignKey(Publisher)
publication_date = models.DateField()
def __str__(self):
return self.title
def __unicode__(self):
return self.title
Now i'm listing all the books using ListView. And on click of a book i'm getting information the book using following method
class BookDetailView(generic.DetailView):
template_name = 'books/book_detail1.html'
model = Book
context_object_name = 'book_detail'
I'm able to access title, pages, price, rating, publisher & publication_date but not getting all the Authors(Author list). While i'm going to simply print it, it prints None in template. I even try to iterate using For Loop but not done in template
views.py
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<ul>
<li>Price:{{ book_detail.price }}</li>
<li>Pub Date:{{ book_detail.publication_date }}</li>
<li>Title: {{ book_detail.title }}</li>
<li>Pages: {{ book_detail.pages }}</li>
<li>Rating: {{ book_detail.rating }}</li>
<li>Publisher: {{ book_detail.publisher }}</li>
<li>Author: {{ book_detail.author }}</li>
</ul>
</body>
</html>
Can anyone help me to out from this?
You have defined a many-to-many relationship between Book and Author which means that a book can have any number of authors. In order to display them you need to loop through the set of authors:
Authors:
<ul>
{% for author in book_detail.author.all %}
<li>{{ author }}</li>
{% endfor %}
</ul>
You might want to change the name of that field to authors to be less confusing.
Alternatively if you want only one author for a book then you need to use a ForeignKey instead of a ManyToManyField. In that case your existing template logic would work.
Or if you use Function-Based Views, define this view:
#views.py
def book_detail_view(request, id):
book = get_object_or_404(Book, id=id)
authors_of_book = book.questions.all()
template = 'books/book_detail1.html'
context = {'authors_of_book': authors_of_book}
return render(request, template, context)
And also in your template:
#html.py
<ul>
{% for author in authors_of_book %}
<li>{{ author }}</li>
{% endfor %}
</ul>
For more detail read this document.

import data of models.py with 2 class into template

I am a amature django developer. I have a model with two class called "Post" and "Catagory". I want to read Category items in my template. How can I import my category in my template and show it's data in my page?
models.py
from django.db import models
from taggit.managers import TaggableManager
class Category(models.Model):
title = models.CharField(max_length=40)
def __unicode__(self):
return self.title
class Post (models.Model):
title = models.CharField(max_length=150)
body = models.TextField()
date = models.DateTimeField()
tags = TaggableManager ()
cats = models.ManyToManyField(Category)
def __unicode__ (self):
return self.title
Thank you.
If you are using class based views and want to list all categories you could do:
# urls.py
url(regex=r'^category/$',
view=CategoriesListView.as_view(),
name='category_list_all'),
# views.py
class CategoriesListView(ListView):
model = Category
# category_list.html
<h2>Category list</h2>
<ul>
{% for cat in category_list %}
<li>
{{ cat.category }}
</li>
{% endfor %}
</ul>
You would place the html file in <project_route>/<app_name>/templates/<app_name>/ or <project_route>/templates/<app_name>/
If you have an existing function based view that's working with the Post model then you can just do something like:
# views.py
...
post = get_object_or_404(Post, pk=pass_in_pk)
return render(request, 'post.html', {'post': post})
# post.html
<h2>Category list</h2>
<ul>
{% for category in post.cats %}
<li>{{ category.title }}</li>
{% endfor %}
</ul>
If you have a class based view based on a Post model using the DetailView then you can also use the above html, just place it in post_detail.html in the appropriate folder.
It`s like get the category value and assign into settings and passed into view html will work
def viewfuncion(request):
template_vars = {}
settings = Category.objects.get(pk=1)
template_vars['title_show'] = settings.title
t = loader.get_template('view.html')
c = Context(template_vars)
return HttpResponse(t.render(c), content_type = "application/xhtml")
So in your HTML { title_show } will print the content

How to use a ForeignKey field as filter in Django?

I have 2 models:
class Category(models.Model):
title = models.CharField(max_length=250)
### other fields
class Album(models.Model):
category = models.ForeignKey(Category)
subject = models.CharField(max_length=200)
### other fields...
.
I just wrote a view for filtering albums by specefic category, also I want them all in home.html template:
#views.py
def commercial(request):
commercial_subjects = Album.objects.filter(category__title__contains="commercial" )
return render(request, 'gallery/index.html', {'commercial_subjects': commercial_subjects})
And it works fine for just commercial category. It seems just like hardcoding if I want to write multiple views for each category like this one. What I need is a view or filtering process which shows all categories and their related album.subject automaticly. So the final result must be like this:
Personal
ALBUM 1
ALBUM 2
Commercial
ALBUM 4
ALBUM5
How can I do that?
Its easy. First of all give a related_name to the foreign key:
class Album(models.Model):
category = models.ForeignKey(Category, related_name='albums')
From view pass all categories:
def myView(request):
categories = Category.objects.all()
return render(request, 'gallery/index.html', {'categories': categories})
Then in template:
<ul>
{% for category in categories %}
<li>{{ category.title }}</li>
{% with category.albums.all as albums %}
{% if albums %}
<ul>
{% for album in albums %}
<li>{{ album.subject }}</li>
{% endfor %}
<ul>
{% endif %}
{% endwith %}
{% endfor %}
</ul>
#views.py
def commercial(request):
commercial_subjects = Album.objects.filter(category__title="commercial")

Categories

Resources