如何仅从 Django 模型中获取最新行并将其显示为 table?

how to fetch only the newest rows from the model in django and show it as a table?

views.py

 def inventory_display(request):
     if request.user.vendor == True and request.user.vendor_approval == True:
         vendor = CustomUser.objects.get(id=request.user.id)
         vendor_product = vendor.vendor_user.all()
         items = vendor_product[0].product_variants.all()
         return render(request, 'vendor/inventory_display.html',{'vendor_product':vendor_product, 'items':items})

Html

                   {% for product in vendor_product %}
                  {% for item in items %}
                    <tr>
                      <th scope="row">{{forloop.counter}}</th>
                      <td>{{product.created|date:"d-m-y"}}</td>
                      <td>{{product.edited|date:"d-m-y"}}</td>
                      <td>{{product.vendoruser}}</td>
                      <td><a href="{% url 'loomerang_admin:product_details' %}">{{product.product_id}}</a></td>
                      <td>{{item.item_num}}</td>
                      <td>{{item.variant_value}}</td>                       
                      <td>{{item.initial_stock}}</td>
                      <td>2</td>
                      <td>{{item.approval_status}}</td>
                      <td>{{item.approved_date|date:"d-m-y"}}</td>
                      <td>{{product.approved_by}}</td>
                    </tr>
                  {% endfor %}
              {% endfor %}

我正在从 3 个不同的模型中获取数据。我确实每次都从这些模型中获取所有数据。如果我只想在添加新行时获取最新行怎么办?我在问题中包含了 User、Product、Productvariants 模型。 我通过 for 循环在模板中显示数据。没有 forloop 我在模板中得到重复的数据,我想要模板中不存在的最新数据。

在您的模型中添加此字段

created_at          = models.DateTimeField(auto_now_add=True)

它会在创建对象时自动添加时间戳。以及当您获取对象时 使用此查询

ModelName.objects.all().order_by('-created_at')

尝试在您的视图中添加索引号:

def inventory_display(request):
     if request.user.vendor == True and request.user.vendor_approval == True:
         vendor = CustomUser.objects.get(id=request.user.id)
         vendor_product = vendor.vendor_user.all()[0] 
         items = vendor_product[0].product_variants.all()[0]
         return render(request, 'vendor/inventory_display.html',{'vendor_product':vendor_product, 'items':items})

并从模板中删除 for 循环:

                <tr>
                  <th scope="row">1</th>
                  <td>{{vendor_product.created|date:"d-m-y"}}</td>
                  <td>{{vendor_product.edited|date:"d-m-y"}}</td>
                  <td>{{vendor_product.vendoruser}}</td>
                  <td><a href="{% url 'loomerang_admin:product_details' %}">{{product.product_id}}</a></td>
                  <td>{{items.item_num}}</td>
                  <td>{{items.variant_value}}</td>                       
                  <td>{{items.initial_stock}}</td>
                  <td>2</td>
                  <td>{{items.approval_status}}</td>
                  <td>{{items.approved_date|date:"d-m-y"}}</td>
                  <td>{{vendor_product.approved_by}}</td>
                </tr>
             

您可以通过多种方式获取最新插入的商品

last() 方法

vendor = CustomUser.objects.last()

order_by() 方法

vendor = CustomUser.objects.order_by('-id').first()

latest() 方法

vendor = CustomUser.objects.latest('id')