How to properly make a query in Django? - python

I've ran into a little problem. I want to construct a proper queryset to get values which represent the number of the expenses per category to display this like that.
This is what I got now:
class CategoryListView(ListView):
model = Category
paginate_by = 5
def get_context_data(self, *, category_object_list=None, **kwargs):
**categories = Category.objects.annotate(Count('expense'))
queryset = categories.values('expense__count')**
return super().get_context_data(
category_object_list=queryset,
**kwargs)
Of course it doesnt work and I have terrible table like this. I suppose the problem isn't in HTML but in my absolutely wrong query... What should I do?
This is my HTML in case it would be needed:
{% for category in object_list %}
<tr>
<td>
{{ category.name|default:"-" }}
</td>
<td>
{% for number in category_object_list %}
{{ number.expense__count }}
{% endfor %}
</td>
<td>
edit
delete
</td>
{% endfor %}
</tr>
Also my models.py:
class Category(models.Model):
name = models.CharField(max_length=50, unique=True)
def __str__(self):
return f'{self.name}'
class Expense(models.Model):
class Meta:
ordering = ('-date', '-pk')
category = models.ForeignKey(Category, null=True, blank=True,
on_delete=models.CASCADE)
name = models.CharField(max_length=50)
amount = models.DecimalField(max_digits=8, decimal_places=2)
date = models.DateField(default=datetime.date.today, db_index=True)
def __str__(self):
return f'{self.date} {self.name} {self.amount}'

You can try like this within your template with the reverse look up of
Foregin Keys. See the docs for more detail.
{% for category in object_list %}
<tr>
<td>
{{ category.name|default:"-" }}
</td>
<td>
{{category.expense_set.all.count}}
</td>
<td>
edit
delete
</td>
{% endfor %}
</tr>
Now in the view you can just pass all the categories with the ListView
class CategoryListView(ListView):
model = Category
paginate_by = 5

Related

Nested Regroups with django with some attributes from none regroups

I have a django template that I have implemented regroups in it. I have a problem with how to display some of the attributes from my model into the template in a table.
I have a couple of issues to address:
How can I display the remaining 4 attributes from the ImplementationMatrix model i.e implementation_status, implementation_summary, challenges, and wayforward to be displayed at the respective columns in the table on the template (index.html) without distorting the layout for each instance of ImplementationMatrix.
The last for loop in the templates displays only one item for subactivity while I have more than subactivity for the respective activity.
Is there any better way of implementing all this?
My Models:
class Strategy(TrackingModel):
"""
Stores all the strategies related to HSSP.
"""
strategy_name = models.CharField(
max_length=255, blank=True, null=True, default="")
class Meta(TrackingModel.Meta):
verbose_name_plural = "Strategies"
def __str__(self):
return self.strategy_name
class Intervention(TrackingModel):
intervention_name = models.CharField(
max_length=255, blank=True, null=True, default=""
)
strategy = models.ForeignKey(Strategy, on_delete=models.PROTECT)
class Meta(TrackingModel.Meta):
verbose_name_plural = "Interventions"
def __str__(self):
return self.intervention_name
class Activity(TrackingModel):
activity_name = models.CharField(
max_length=255, blank=True, null=True, default="")
intervention = models.ForeignKey(Intervention, on_delete=models.PROTECT)
class Meta(TrackingModel.Meta):
verbose_name_plural = "Activities"
def __str__(self):
return self.activity_name
class SubActivity(TrackingModel):
subactivity_name = models.CharField(
max_length=255, blank=True, null=True, default=""
)
activity = models.ForeignKey(Activity, on_delete=models.PROTECT)
class Meta(TrackingModel.Meta):
verbose_name_plural = "Sub Activities"
def __str__(self):
return self.subactivity_name
class ImplementationMatrix(TrackingModel):
"""
The class for keeping track the implementation of each action plan
"""
IMPLEMENTATION_STATUS = (
("ON PROGRESS", "ON PROGRESS"),
("DONE", "DONE"),
("NOT DONE", "NOT DONE"),
)
implementation_summary = models.TextField()
implementation_status = models.CharField(
choices=IMPLEMENTATION_STATUS, max_length=100, default="",
verbose_name="Matrix Implementation Status"
)
challenges = models.TextField()
way_forward = models.TextField()
sub_activity = models.ForeignKey(
SubActivity, on_delete=models.PROTECT, verbose_name='Sub-Actions')
def __str__(self) -> str:
return self.sub_activity.activity.intervention.strategy.strategy_name
My View:
#login_required(login_url="/login")
def matrix_implementation_list(request: HttpRequest) -> HttpResponse:
all_implementations = ImplementationMatrix.objects.filter(
active=True, deleted=False).select_related(
'sub_activity',
'sub_activity__activity',
'sub_activity__activity__intervention',
'sub_activity__activity__intervention__strategy')
context = {'all_implementations': all_implementations}
return render(request, 'index.html', context)
My template (index.html)
<div class="card-body">
{% regroup all_implementations by sub_activity.activity.intervention.strategy.strategy_name as strategy_list %}
<!-- Start Table section -->
<div class="table-responsive table-bordered">
<table class="table">
<thead class="thead-light">
<tr>
<th>Policy Statement</th>
<th>Action</th>
<th>Sub-Actions</th>
<th>Implementation Summary</th>
<th>Implementation Status</th>
<th>Challenges</th>
<th>Way Forward</th>
</tr>
</thead>
{% for strategy in strategy_list %}
<tr>
<td colspan="7" class="text-center"><strong>Priority Area {{ forloop.counter }}: {{ strategy.grouper }}</strong></td>
</tr>
{% regroup strategy.list by sub_activity.activity.intervention.intervention_name as intervention_list %}
{% for intervention in intervention_list %}
{% regroup intervention.list by sub_activity.activity.activity_name as activity_list %}
{% for activity in activity_list %}
{% regroup activity.list by sub_activity.subactivity_name as subactivity_list %}
{% for subactivity in subactivity_list %}
<tr>
{% if forloop.first %}
{% if forloop.parentloop.first %}
<td rowspan="{{ intervention.list|length }}">
{{ intervention.grouper|wordwrap:30|linebreaksbr }}
</td>
{% endif %}
<td rowspan="{{ activity.list|length }}">
{{ activity.grouper|wordwrap:30|linebreaksbr }}
</td>
{% endif %}
<td>
{{ subactivity.grouper|wordwrap:20|linebreaksbr }}
</td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
{% endfor %}
{% endfor %}
{% endfor %}
{% endfor %}
</table>
</div>
<!-- end Table Section -->
</div>
I was able to display the contents of the parent model in the last 'for loop' by doing the following:
<td>{{ subactivity.grouper|wordwrap:20|linebreaksbr }}</td>
<td>{{subactivity.list.0.implementation_summary|wordwrap:30|linebreaksbr}}</td>
<td>{{ subactivity.list.0.get_implementation_status_display }}</td>
<td>{{ subactivity.list.0.challenges|wordwrap:30|linebreaksbr }}</td>
<td>{{ subactivity.list.0.way_forward|wordwrap:30|linebreaksbr }}</td>

i have to make a relation between movie and actor without using manytomany field i have to use only foreign key in django i've write this code so far

models.py
class Movielist(models.Model) :
Title = models.CharField(max_length=1000)
Description = models.TextField(blank=True)
ReleaseDate = models.DateTimeField(verbose_name='Release Date', blank=True)
# NoOfActors = models.IntegerField()
Upvote = models.IntegerField(default=0)
Downvote = models.IntegerField(default=0)
def __str__(self):
return self.Title
class Actorlist(models.Model):
Name = models.CharField(max_length=1000)
DateofBirth = models.DateTimeField(verbose_name='Date of Birth',blank=True)
# NoOfActors = models.IntegerField()
def __str__(self):
return self.Name
class ActorInMovie(models.Model):
Movie = models.ForeignKey(Movielist, default=1, on_delete=models.CASCADE, blank=True)
Actor = models.ForeignKey(Actorlist, default=1, on_delete=models.CASCADE, blank=True)
def __str__(self):
return self.Movie.Title
views.py
def Movie_Detail(request):
MovieName = Movielist.objects.all()
tablelist = ActorInMovie.objects.all()
return render(request, 'Collection/index.html', {'MovieName':MovieName, 'tablelist':tablelist})
index.html
<table border="solid">
<th>Name</th>
<th>Release Date</th>
<th>Actors</th>
{% for data in MovieName %}
<tr>
<td>{{ data.Title }}</td>
<td>{{ data.ReleaseDate }}</td>
<td>
<ul>
{% for name in tablelist %}
<li>{{ name.Actor.Name }}</li>
{% endfor %}
</ul>
</td>
</tr>
{% endfor %}
</table>
**i have getting this out put can any any one tell me how to filter this data only my movie id i would like if someone come and help me in solving this problem
[this is the output what i am getting but i want filter actors name by movielist.id][1]
[1]: https://i.stack.imgur.com/BlAuP.png**
You are selecting all the objects in ActorInMovie into tablelist, and not just the related ones. You don't need the tablelist at all. Instead:
{% for data in MovieName %}
<tr>
<td>{{ data.Title }}</td>
<td>{{ data.ReleaseDate }}</td>
<td>
<ul>
{% for movie_actor in data.ActorInMovie %}
<li>{{ movie_actor.Actor.Name }}</li>
{% endfor %}
</ul>
</td>
</tr>
{% endfor %}
You might also need to use related_name in your ActorInMovie model to tell django how you'll identify the related fields:
Movie = models.ForeignKey(Movielist, default=1, on_delete=models.CASCADE, blank=True, related_name='ActorInMovie)
Since you're going to print all the actors of every movie, it's a good idea to use prefetch_related(Another ref) to achieve better performance, but it's not required if you don't have a lot of data.

Django prefetch_related outputs None

I'm new to Django. I am making a simple store.
Currently I am working on the Order section.
Every Order has Order Items inside it. Every order item has some values and a product id.
What I am trying to display on the index.html, is the orders and its items inside it. However order.items always outputs order.OrderItem.None
views.py
class IndexView(generic.ListView):
template_name = 'order/index.html'
context_object_name = 'all_orders'
def get_queryset(self):
return Order.objects.all().prefetch_related('items')
def get_context_data(self, **kwargs):
context = super(IndexView, self).get_context_data(**kwargs)
return context
views.py
# Create your models here.
class Order(models.Model):
user = models.ForeignKey(User, related_name='orders')
created_at = models.DateTimeField(auto_now_add=True, null=True)
class OrderItem(models.Model):
product = models.ForeignKey(Product)
order = models.ForeignKey(Order, related_name='items')
item_name = models.CharField(max_length=255, null=True, blank=True)
item_price_in_usd = models.DecimalField(max_digits=6, decimal_places=2, null=True, blank=True)
def __str__(self):
return self.product.name
index.html
{% for order in all_orders %}
<tr>
<td>{{ order}}</td>
<td>{{ order.created_at}}</td>
<td>{{ order.items}}</td>
</tr>
{% endfor %}
Ok, I have found to solution. Apparently you have to add .all
{% for order in all_orders %}
<tr>
<td>{{ order}}</td>
<td>{{ order.created_at}}</td>
<td>
{% for items in order.items.all %}
<td>{{ items.item_name}}</td>
{% endfor %}
</td>
</tr>
{% endfor %}

Django - passing model object datas to template

I could'nt solve it. I want to pass specific datas from model to template.
I had tried something could'nt figure it out.Can anyone help me out , should I need to write logic to view.py or is there easy way to pass data.
class Receipt(models.Model):
amount = models.DecimalField(max_digits=5, decimal_places=2)
vat = models.DecimalField(max_digits=5, decimal_places=2)
total_amount = models.DecimalField(max_digits=5, decimal_places=2)
def __str__(self):
return str(self.total_amount)
#My view.py
class IndexView(TemplateView):
template_name = "index.html"
model = Receipt
class DetailView(TemplateView):
template_name = "detail.html"
model = ReceiptItem
#index.html
<div class="panel panel-default">
<div class="panel-heading">Market</div>
<table class="table">
<tr>
{% for amount in objects %}
<td>{{ amount }}</td>
<td>{{ vat }}</td>
<td>{{ total_amount }}</td>
{% endfor %}
</tr>
</table>
</div>
TemplateView does not provide support for models. If you want a list of all of the objects so you can loop over them you will want a ListView, then switch your for loop to be:
{% for amount in object_list %}
https://docs.djangoproject.com/en/1.10/ref/class-based-views/generic-display/

Iterating through Django ModelForm Instance in Template

I am iterating through a queryset in the template using template tags to show the data of existing database entries (i.e. Customer Orders). However, I want to allow users to edit one of these fields (i.e. Delivery Remarks) for each Customer Order.
My approach was to use ModelForm but I cannot iterate the instances for each form as I iterate through the Customer Orders in the template.
I tried to iterate through an instance to a ModelForm but I get stuck because I am unable to pass the instance to a ModelForm in a template when in context(in views.py) it is the entire queryset that is passed to the template, not an instance. Perhaps I am approaching this problem the wrong way.
My code is below and I am thankful for any help you can give:
Models.py
from django.db import models
from products.models import Product
from counters.models import Counter
from promo.models import Promo
from django.contrib.auth.models import User
class Order(models.Model):
order_status = models.ForeignKey('OrderStatus')
products = models.ManyToManyField(Product, through='OrderProductDetails', through_fields=('order','product'), null=True, blank=True)
counter = models.ForeignKey(Counter, null=True, blank=True)
order_type = models.ForeignKey('OrderType')
order_remarks = models.CharField(max_length=1000, null=True, blank=True)
order_date = models.DateTimeField(auto_now_add=True, auto_now=False)
ordered_by = models.ForeignKey(User, null=True, blank=True)
promo = models.ForeignKey('promo.Promo', verbose_name="Order for which Promotion (if applicable)", null=True, blank=True)
delivery_date = models.DateField(blank=True, null=True)
delivery_remarks = models.CharField(max_length=1000, null=True, blank=True)
updated_on = models.DateTimeField(auto_now_add=False, auto_now=True)
class Meta:
verbose_name = "Order"
verbose_name_plural = "*Orders*"
def __unicode__(self):
return str(self.id)
class OrderProductDetails(models.Model):
order = models.ForeignKey('Order')
product = models.ForeignKey('products.Product')
quantity = models.PositiveIntegerField()
selling_price = models.DecimalField(decimal_places=2, max_digits=10)
order_product_remarks = models.ForeignKey('OrderProductRemarks',blank=True, null=True)
class Meta:
verbose_name_plural = "Order - Product Details"
verbose_name = "Order - Product Details"
def __unicode__(self):
return str(self.id)
class OrderProductRemarks(models.Model):
order_product_remarks = models.CharField(max_length=240, null=False, blank=False)
class Meta:
verbose_name_plural = "Order Product Remarks"
def __unicode__(self):
return str(self.order_product_remarks)
class OrderStatus(models.Model):
order_status_number = models.PositiveIntegerField(null=False, blank=False)
order_status = models.CharField(max_length=100, null=False, blank=False)
class Meta:
verbose_name_plural = "Order Status"
def __unicode__(self):
return str(self.order_status_number) + ". " + str(self.order_status)
class OrderType(models.Model):
order_type = models.CharField(max_length=100, null=False, blank=False)
class Meta:
verbose_name_plural = "Order Type"
def __unicode__(self):
return str(self.order_type)
Views.py
from django.shortcuts import render
from django.contrib.auth.decorators import login_required, user_passes_test
from django.contrib.admin.views.decorators import staff_member_required
from orders.models import Order
from orders.forms import OrderForm, RemarksForm
from products.models import Product
#login_required(login_url='/admin/login/?next=/')
def warehouseOrders(request):
queryset = Order.objects.filter(order_status__order_status_number = 2) #Filter through default queryset manager with filter through FK
form = RemarksForm(request.POST or None)
if form.is_valid():
form.save()
context = {'queryset': queryset, 'form': form}
template = 'warehouse_orders.html'
return render(request, template, context)
Forms.py
from django import forms
from .models import Order
class RemarksForm(forms.ModelForm):
class Meta:
model = Order
fields = ['delivery_remarks']
Template.html
{% extends 'base_frontend.html' %}
{% load crispy_forms_tags %}
{% block head_title %}
({{ queryset|length}}) Warehouse Orders
{% endblock %}
{% block head_styles %}
{% endblock %}
{% block jquery %}
{% endblock %}
{% block content %}
<h1>Orders to Pack</h1>
<br>
{% for item in queryset %}
Order ID: {{ item }}<br>
<b>Order Status: {{ item.order_status }}</b><br>
Counter: {{ item.counter }}<br>
Order Type: {{ item.order_type }}<br>
Order Remarks: {{ item.order_remarks }}<br>
Order Date: {{ item.order_date }}<br>
Sales Rep: {{ item.ordered_by }}<br>
Promo: {{ item.promo }}<br>
Delivery Date: {{ item.delivery_date }}<br>
<table class="table table-striped table-bordered">
<tr>
<th class="bottom-align-th">#</th>
<th class="bottom-align-th">Article No.</th>
<th class="bottom-align-th">Barcode No.</th>
<th class="bottom-align-th">Color</th>
<th class="bottom-align-th">Free Size</th>
<th class="bottom-align-th">3MTH<br>110<br>S</th>
<th class="bottom-align-th">6MTH<br>120<br>M</th>
<th class="bottom-align-th">9MTH<br>130<br>L</th>
<th class="bottom-align-th">------<br>140<br>XL</th>
<th class="bottom-align-th">------<br>150<br>XXL</th>
<th class="bottom-align-th">Unit Price</th>
<th class="bottom-align-th">Total Quantity</th>
<th class="bottom-align-th">Remarks</th>
</tr>
{% for product in item.products.all %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ product.article_number }}</td>
<td>{{ product.barcode }}</td>
<td>{{ product.color }}</td>
<td>{{ product.quantity }}</td>
</tr>
{% endfor %}
</table>
<br>
Delivery Remarks: {{ item.delivery_remarks }}<br>
{% if form %}
<form method="POST" action=""> {% csrf_token %}
{{ form|crispy }}
<input type="submit" value="Save" class="btn btn-default"/>
</form>
{% endif %}
<br>
<button class="btn btn-success btn-lg">Start Packing</button>
<button class="btn btn-primary btn-lg">Finish Packing</button>
<button class="btn btn-danger btn-lg">Send Order to HQ for Changes</button>
{% endfor %}
{% endblock %}
Btw, using Django 1.7.2 here.

Categories