Answer the question
In order to leave comments, you need to log in
How to pass each work object from the ListView to the Model Manager?
Hello! I hope for your help.
There is a site for daily rent. I am making a page for employees where orders are displayed.
In template:
{% for order in order_list %}
<p>Дата заказа: {{ order.timestamp }}</p>
<p>Юзер: {{ order.user.email }}</p>
{% for item in order.cart.items.all %}
<li>{{ item.get_title }} <b>{{ item.rentdate_set.get_cart_dates }}</b></li>
{% endfor %}
{% endfor %}
class RentDateManager(models.Manager):
...
def all(self, *args, **kwargs):
return self.get_queryset()
def get_cart_dates(self, instance):
cart_dates = self.get_queryset().filter(cart=instance.cart)
return cart_dates
class RentDate(models.Model):
startCar = models.DateField(blank=True, null=True)
endCar = models.DateField(blank=True, null=True)
user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True)
variation = models.ForeignKey('Variation', null=True, blank=True)
cart = models.ForeignKey(Cart, null=True, blank=True)
objects = RentDateManager()
class StaffOrderList(StaffRequiredMixin, ListView):
model = Order
queryset = Order.objects.all()
template_name = "staff_order_list.html"
def get_queryset(self):
queryset = super(StaffOrderList, self).get_queryset().order_by('-timestamp')
return queryset
def get_context_data(self, *args, **kwargs):
context = super(StaffOrderList, self).get_context_data(*args, **kwargs)
return context
Answer the question
In order to leave comments, you need to log in
It's easier to write a method for Item'a
class CardItem(models.Model):
....
def get_rent_dates(self):
return RentDate.objects.filter(cart=self.cart)
{% for item in order.cart.items.all %}
{{ item.get_rent_dates }}
{% endfor %}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question