0

在向查询集添加更多数据后,我在我的 html 中添加了 for 循环。出于某种原因,它显示了我通过的 3 个值中的 1 个。- 我可以打印它们,只是没有显示在 html 上。

结果页面 - client_name(蓝色),缺少 product_name + waiting_time(红色) 在此处输入图像描述

有任何想法吗?谢谢你

模型.py

class Orders(models.Model):
created_date = models.DateTimeField(auto_now_add=True)
client_id = models.IntegerField()
client_name = models.CharField(max_length=50, blank=True, null=True)
delivery_city = models.CharField(max_length=50, blank=True, null=True)
delivery_address = models.CharField(max_length=50, blank=True, null=True)
product_id = models.IntegerField()
sold_by = models.CharField(max_length=20)
sale_type = models.CharField(max_length=20, blank=True, null=True)
units_to_buy = models.IntegerField(blank=True, null=True)
order_cost = models.DecimalField(decimal_places=2, max_digits=100000, blank=True, null=True)
payment_method = models.CharField(max_length=20, blank=True, null=True)
payment_channel = models.CharField(max_length=20, blank=True, null=True)
invoice = models.CharField(max_length=20, blank=True, null=True)
delivery_cost = models.DecimalField(decimal_places=2, max_digits=100000, blank=True, null=True)
delivery_type = models.CharField(max_length=20, blank=True, null=True)
delivery_status = models.CharField(max_length=20, default='ממתין למשלוח')
delivery_person_name = models.CharField(max_length=20, blank=True, null=True)
notes = models.TextField(blank=True, null=True)
modified_date = models.DateTimeField(auto_now=True)

def __str__(self):
    return '{}'.format(self.client_name + ' - (' + self.sold_by + ')')

class Clients(models.Model):
first_name = models.CharField(max_length=20)
last_name = models.CharField(max_length=20, blank=True, null=True)
client_type = models.CharField(max_length=15, blank=True, null=True)
company_name = models.CharField(max_length=30, blank=True, null=True)
address = models.CharField(max_length=30, blank=True, null=True)
city = models.CharField(max_length=15, blank=True, null=True)
phone_number = models.CharField(max_length=10)
additional_phone_number = models.CharField(max_length=10, blank=True, null=True)
email = models.EmailField(max_length=30, blank=True, null=True)
card_type = models.CharField(max_length=15, blank=True, null=True)
four_digits = models.IntegerField(blank=True, null=True)
notes = models.TextField(blank=True, null=True)
added = models.DateTimeField(auto_now=True)
created = models.DateTimeField(auto_now=True)

def __str__(self):
    return '{}'.format(self.first_name)

class Products(models.Model):
product_name = models.CharField(max_length=50, verbose_name=u'שם המוצר')
supplier_name = models.CharField(max_length=20, blank=True, null=True)
purchase_price = models.DecimalField(decimal_places=2, max_digits=100000, blank=True, null=True)
purchase_price_before_fees = models.DecimalField(decimal_places=2, max_digits=100000, blank=True, null=True)
final_price = models.DecimalField(decimal_places=2, max_digits=100000, blank=True, null=True)
final_price_before_fees = models.DecimalField(decimal_places=2, max_digits=100000, blank=True, null=True)
inventory_in = models.IntegerField(blank=True, null=True)
inventory_out = models.IntegerField(blank=True, null=True)
inventory_total = models.IntegerField(blank=True, null=True)
notes = models.TextField(blank=True, null=True)
created = models.DateTimeField(auto_now=True)

def __str__(self):
    return '{}'.format(self.product_name)

URLs.py

...
path('orders/', views.orders_filter_view, name='orders'),
...

视图.py

def orders_filter_view(request):
   qs = models.Orders.objects.all()
...
for order in qs:
    client = models.Clients.objects.get(pk=order.client_id)
    client_name = client.first_name
    qs.client_name = client_name      <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    product = models.Products.objects.get(pk=order.product_id)
    product_name = product.product_name
    qs.product_name = product_name    <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    tz_info = order.created_date.tzinfo
    qs.waiting_time = order.created_date - datetime.datetime.now(tz_info)     <<<<<<<<<<<<<<<<<<<<<<<

total_matches = qs.count()

context = {
    'orders': qs,     <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    'view': 'הזמנות',
    'total_matches': total_matches,
    'titles': orders_titles
}

return render(request, 'adolim/orders.html', context)

订单.html

{% for order in orders %}
                    <tr>
                        <td class="center">{{ order.client_name }}</td>    <<<<<<<<<<<<<<<<<<<<<<<<
                        <td class="center">{{ order.product_name }}</td>    <<<<<<<<<<<<<<<<<<<<<<<<
                        ...
                        <td class="center">{{ order.delivery_person_name }}</td>
                        {% if order.delivery_status == 'סופק' %}
                            <td class="center green-text">{{ order.delivery_status }}</td>
                        {% else %}
                            <td class="center">{{ order.delivery_status }}</td>
                        {% endif %}
                        <td class="center yellow-text">{{ order.waiting_time }}</td>   <<<<<<<<<<<<<<<<<

结果页面 - client_name(蓝色),缺少 product_name + waiting_time(红色) 在此处输入图像描述

4

1 回答 1

0

你做错的一件事是你在 for 循环中运行查询,这是一个大问题,而不是我建议你创建一个带有连接表的查询,一种方法是使用select_related或我喜欢使用的另一种方法:主要values区别是select_related返回具有所有字段的对象,同时values将返回具有指定字段的字典数组。有关https://docs.djangoproject.com/en/3.0/ref/models/querysets/#select-related的更多信息select_related

# I am not sure how your Models looks like but I assume it is One-to-Many Clients-Orders

def orders_filter_view(request):
   # client, product in quotes is the foreign key field name
   qs = models.Orders.objects.select_related('client', 'product').all()
...
    # I am not sure what are you trying to do with dates
    # But now you can easily access any data from foreign key objects without hitting DB again
    for order in qs:
        print(order.client.first_name)
        print(order.product.product_name)
        tz_info = order.created_date.tzinfo
        qs.waiting_time = order.created_date - datetime.datetime.now(tz_info)     

于 2020-06-19T01:11:14.090 回答