F
F
filmincer2016-07-02 15:50:28
Django
filmincer, 2016-07-02 15:50:28

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 %}

Each item in the cart has dates for which it is rented.
I want to display the dates filtered by the cart. To do this, for the RentDate model, I wrote a method in the Model.Manager get_cart_dates :
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()

Such a view:
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

Then I realized that I absolutely do not know how to actually pass the instance from the template (or call it from another place). Such things were obtained in DetailView, but in ListView I got stuck.
Thanks in advance for any help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Podgursky, 2016-07-07
@kmmbvnr

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 question

Ask a Question

731 491 924 answers to any question