我整天都被困在这一天。我有一个模型,Load,在该模型中它有一个具有不同“状态”的选择字段。我正在尝试在我的模板中实现一些选项卡式视图。所以基本上每个标签都将负责显示某个状态。我相信我应该在我的视图中使用查询集get_context_data,但我真的不知道如何实现这一点。我在视图中尝试了很多不同的方法,所以我只是粘贴最新的尝试。
这些查询在完成后将是静态的,所以我最好的想法是为每个查询使用模型管理器,但我无法让多个模型管理器在一个模型上工作。这是一个例子。任何帮助,将不胜感激。谢谢
class BookedManager(models.Manager):
def get_queryset(self):
return super(BookedManager, self).get_queryset().filter(load_status='Booked')
的 Models.py
class Load(models.Model):
load_number = models.IntegerField()
agent = models.ForeignKey(User,
limit_choices_to={'groups__name': 'Agent'},related_name='Agent', on_delete=models.CASCADE, null=True)
customer = models.ForeignKey(User,
limit_choices_to={'groups__name': 'Customer'},related_name='Customer', on_delete=models.CASCADE)
carrier = models.ForeignKey(Carrier, on_delete=models.CASCADE, blank=True, null=True)
pickup_date = models.DateField()
delivery_date = models.DateField()
shipper = models.ForeignKey(Location, related_name='shipper', on_delete=models.CASCADE, blank=True)
consignee = models.ForeignKey(Location, related_name='consignee', on_delete=models.CASCADE, blank=True)
po_number = models.CharField(max_length=255)
pu_number = models.CharField(max_length=255)
pieces = models.IntegerField()
description = models.TextField()
date_created = models.DateTimeField(auto_now_add=True)
PREBOOK = 'Waiting for Carrier'
BOOKED = 'Booked'
CONFIRMED = 'Confirmed Rate'
PICKED = 'Picked'
DELIVERED = 'Delivered'
INVOICED = 'Invoiced'
PAID = 'Paid'
LOAD_STATUS_CHOICES = (
(PREBOOK, 'Waiting for Carrier'),
(BOOKED, 'Booked'),
(CONFIRMED, 'Signed Rate Confirmation'),
(PICKED, 'Picked Up'),
(DELIVERED, 'Delivered'),
(INVOICED, 'Invoiced'),
(PAID, 'Paid'),
)
load_status = models.CharField(max_length=30, choices=LOAD_STATUS_CHOICES, default=PREBOOK)
def save(self, *args, **kwargs):
LoadUpdateHistory.objects.create(your_model=self, updated=timezone.now())
def get_absolute_url(self):
return reverse('loads:detail',kwargs={'pk':self.pk})
def __str__(self):
return str(self.load_number)
views.py
class LoadListView(ListView):
model = Load
context_object_name = 'loads'
def get_context_data(self, **kwargs):
context = super(LoadListView, self).get_context_data(**kwargs)
context['booked'] = Load.objects.filter(load_status__contains='booked')
return context
模板
{% for load in loads %}
<tr>
<td><a href="{% url 'loads:detail' pk=load.pk%}">{{load.load_number}}</a></td>
<td>{{load.agent}}</td>
<td>{{user.customer.company_name}}</td>
<td>{{load.carrier}}</td>
<td>{{load.pickup_date}}</td>
<td>{{load.delivery_date}}</td>
<td>{{load.shipper}}</td>
<td>{{load.consignee}}</td>
<td>{{load.po_number}}</td>
<td>{{load.pu_number}}</td>
<td>{{load.date_created}}</td>
<td>
<a href="#">
<button class="btn btn-warning" type="button">
<span class="glyphicon glyphicon-pencil"></span> </button>
</a>
<a href="{% url 'loads:delete' pk=load.pk%}">
<button class="btn btn-danger" type="button">
<span class="glyphicon glyphicon-remove"></span> </button>
</a>
</td>
</tr>
{% endfor %}
更新1/21/17
Views.py
class LoadListView(ListView):
model = Load
context_object_name = 'loads'
def get_context_data(self, **kwargs):
context = super(LoadListView, self).get_context_data(**kwargs)
"""
This was probably my biggest mistake, I kept trying to query from
Load.load_status, load_status doesn't have a choice, it holds a variable
which then holds a status.
which i'm assuming is why i can't query off of the object load_status.
This new Loop says for the first choice in Load.LOAD_STATUS_CHOICES.
I'm not really understanding why we are selecting the first choice here.
"""
statuses = (choice[0] for choice in Load.LOAD_STATUS_CHOICES)
"""
Then we pass in loads_by_status into the template it filters the load_status
object and gets the status in statuses.
"""
context['loads_by_status'] = {
status: Load.objects.filter(load_status=status) for status in statuses
}
return context
我添加此代码的结果是模板没有返回任何内容,所以我必须在模板中调用错误的数据。
模板
<div>
<!-- Nav tabs -->
<ul class="nav nav-tabs" role="tablist" id='myTab'>
<li role="presentation" class="active"><a href="#allLoads" aria-controls="allLoads" role="tab" data-toggle="tab">All Loads</a></li>
<li role="presentation"><a href="#waitingForCarrier" aria-controls="waitingForCarrier" role="tab" data-toggle="tab">Waiting For Carrier</a></li>
<li role="presentation"><a href="#booked" aria-controls="booked" role="tab" data-toggle="tab">Booked</a></li>
<li role="presentation"><a href="#confirmed" aria-controls="confirmed" role="tab" data-toggle="tab">Confirmed</a></li>
<li role="presentation"><a href="#picked" aria-controls="picked" role="tab" data-toggle="tab">Picked</a></li>
<li role="presentation"><a href="#delivered" aria-controls="delivered" role="tab" data-toggle="tab">Delivered</a></li>
<li role="presentation"><a href="#invoiced" aria-controls="invoiced" role="tab" data-toggle="tab">Invoiced</a></li>
<li role="presentation"><a href="#paid" aria-controls="paid" role="tab" data-toggle="tab">Paid</a></li>
</ul>
<!-- Tab panes -->
<div class="tab-content">
<div role="tabpanel" class="tab-pane fade in active" id="allLoads">
<h1>All Loads</h1>
<table id="example" class="table table-striped table-bordered" cellspacing="0"
width="100%">
<thead>
<tr>
<th>Load Number</th>
<th>Load Status</th>
<th>Agent</th>
<th>Customer</th>
<th>Carrier</th>
<th>Pickup Date</th>
<th>Delivery Date</th>
<th>Shipper</th>
<th>Consignee</th>
<th>PO Number</th>
<th>PU Number</th>
<th>Date Created</th>
<th>Action</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Load Number</th>
<th>Load Status</th>
<th>Agent</th>
<th>Customer</th>
<th>Carrier</th>
<th>Pickup Date</th>
<th>Delivery Date</th>
<th>Shipper</th>
<th>Consignee</th>
<th>PO Number</th>
<th>PU Number</th>
<th>Date Created</th>
<th>Action</th>
</tr>
</tfoot>
<tbody>
{% for load in loads %}
<tr>
<td><a href="#">{{load.load_number}}</a></td>
<td>{{load.load_status}}</td>
<td>{{load.agent}}</td>
<td>{{user.customer.company_name}}</td>
<td>{{load.carrier}}</td>
<td>{{load.pickup_date}}</td>
<td>{{load.delivery_date}}</td>
<td>{{load.shipper}}</td>
<td>{{load.consignee}}</td>
<td>{{load.po_number}}</td>
<td>{{load.pu_number}}</td>
<td>{{load.date_created}}</td>
<td>
<a href="#">
<button class="btn btn-warning" type="button">
<span class="glyphicon glyphicon-pencil"></span> </button>
</a>
<a href="#">
<button class="btn btn-danger" type="button">
<span class="glyphicon glyphicon-remove"></span> </button>
</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div role="tabpanel" class="tab-pane fade" id="waitingForCarrier">
<h1>Waiting For Carrier</h1>
<table id="example" class="table table-striped table-bordered" cellspacing="0"
width="100%">
<thead>
<tr>
<th>Load Number</th>
<th>Load Status</th>
<th>Agent</th>
<th>Customer</th>
<th>Carrier</th>
<th>Pickup Date</th>
<th>Delivery Date</th>
<th>Shipper</th>
<th>Consignee</th>
<th>PO Number</th>
<th>PU Number</th>
<th>Date Created</th>
<th>Action</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Load Number</th>
<th>Load Status</th>
<th>Agent</th>
<th>Customer</th>
<th>Carrier</th>
<th>Pickup Date</th>
<th>Delivery Date</th>
<th>Shipper</th>
<th>Consignee</th>
<th>PO Number</th>
<th>PU Number</th>
<th>Date Created</th>
<th>Action</th>
</tr>
</tfoot>
<tbody>
{% for status, load in loads_by_status.items %}
<tr>
<td><a href="#">{{load.load_number}}</a></td>
<td>{{load.load_status}}</td>
<td>{{load.agent}}</td>
<td>{{user.customer.company_name}}</td>
<td>{{load.carrier}}</td>
<td>{{load.pickup_date}}</td>
<td>{{load.delivery_date}}</td>
<td>{{load.shipper}}</td>
<td>{{load.consignee}}</td>
<td>{{load.po_number}}</td>
<td>{{load.pu_number}}</td>
<td>{{load.date_created}}</td>
<td>
<a href="#">
<button class="btn btn-warning" type="button">
<span class="glyphicon glyphicon-pencil"></span> </button>
</a>
<a href="#">
<button class="btn btn-danger" type="button">
<span class="glyphicon glyphicon-remove"></span> </button>
</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div role="tabpanel" class="tab-pane fade" id="booked">
<h1>Booked</h1>
<table id="example" class="table table-striped table-bordered" cellspacing="0"
width="100%">
<thead>
<tr>
<th>Load Number</th>
<th>Load Status</th>
<th>Agent</th>
<th>Customer</th>
<th>Carrier</th>
<th>Pickup Date</th>
<th>Delivery Date</th>
<th>Shipper</th>
<th>Consignee</th>
<th>PO Number</th>
<th>PU Number</th>
<th>Date Created</th>
<th>Action</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Load Number</th>
<th>Load Status</th>
<th>Agent</th>
<th>Customer</th>
<th>Carrier</th>
<th>Pickup Date</th>
<th>Delivery Date</th>
<th>Shipper</th>
<th>Consignee</th>
<th>PO Number</th>
<th>PU Number</th>
<th>Date Created</th>
<th>Action</th>
</tr>
</tfoot>
<tbody>
{% for status, load in loads_by_status.items %}
<tr>
<td><a href="#">{{load.load_number}}</a></td>
<td>{{load.load_status}}</td>
<td>{{load.agent}}</td>
<td>{{user.customer.company_name}}</td>
<td>{{load.carrier}}</td>
<td>{{load.pickup_date}}</td>
<td>{{load.delivery_date}}</td>
<td>{{load.shipper}}</td>
<td>{{load.consignee}}</td>
<td>{{load.po_number}}</td>
<td>{{load.pu_number}}</td>
<td>{{load.date_created}}</td>
<td>
<a href="#">
<button class="btn btn-warning" type="button">
<span class="glyphicon glyphicon-pencil"></span> </button>
</a>
<a href="#">
<button class="btn btn-danger" type="button">
<span class="glyphicon glyphicon-remove"></span> </button>
</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
答案 0 :(得分:0)
根本不需要这些查询的自定义模型管理器。您可以完成此操作,将按状态分组的载荷发送到模板:
{% for status, loads in loads_by_status.items %}
<h4>Status: {{ status }}</h4>
<table>
<thead>
<tr>
<th>Load number</th>
...
</tr>
</thead>
<tbody>
{# remember, loads is a queryset at this point #}
{% for load in loads %}
<tr>
<td>{{ load.load_number }}</td>
...
</tr>
{% endfor %}
</tbody>
</table>
{% endfor %}
模板:
<bean id="student1" class="com.spring.assin2.Student">
<property name="name" value="ram"></property>
<property name="id" value="1"></property>
<property name="listTest">
<list value-type="java.util.List">
<ref bean="test1"/>
<ref bean="test2"/>
</list>
</property>
</bean>