查找两个日期之间的模型对象实例

时间:2018-10-25 08:41:54

标签: python django

我想要我的购买记录。当我在购买模型中添加购买项目时,该数据显示在我的另一个应用程序记录中,其中日期时间字段的开始日期为结束日期。如果我们将两个日期相加,则会在记录中显示我们在这些日期之间购买的所有购买数据。 所以请帮我谢谢。

下面的代码是我的项目应用名称购买,型号名称是PurchaseReport。

from django.db import models
from stock.models import *

class PurchaseReport(models.Model):
    product_name = models.CharField(max_length=100, blank=False)
    quantity = models.IntegerField()
    price = models.DecimalField(max_digits=12, decimal_places=2)
    #status = models.CharField(max_length=10, default='Purchased')

    def save(self):
        if not self.pk:
            Stock.objects.create(product_name=self.product_name, quantity=self.quantity, price=self.price)
        super().save()

    def __str__(self):
    return 'Product name:{0} Quantity:{1} Price:{2}'.format(self.product_name, self.quantity, self.price)

    class Meta:
        ordering = ['product_name']

这是我购买应用程序的views.py

from django.shortcuts import render,redirect,get_object_or_404
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from .models import *
from stock.models import *
from .forms import *


def display_purchase_report(request):
    items_list = PurchaseReport.objects.all()
    query = request.GET.get("q")
    if query:
        items_list = items_list.filter(product_name__icontains=query)
    paginator = Paginator(items_list, 5)

    page = request.GET.get('page')
    try:
        items = paginator.page(page)
    except PageNotAnInteger:
        items = paginator.page(1)
    except EmptyPage:
        items = paginator.page(paginator.num_pages)
    context = {
        'object_list': items,
        'items': items_list,
        'header': 'PURCHASED ITEMS'
    }
    return render(request, 'index3.html', context)


def add_purchase_report(request):
    if request.method == "POST":
        form = PurchaseReportForm(request.POST)

        if form.is_valid():
            form.save()
            return redirect('display_purchase_report')

    else:
        form = PurchaseReportForm()
        return render(request, 'add_new3.html', {'form': form})

下一个是另一个应用程序记录,型号名称是Records。

from django.db import models
from purchase_report.models import *


class Records(models.Model):
    start_date = models.DateTimeField(null=True)
    end_date = models.DateTimeField(null=True)

    def __str__(self):
        return 'Start Date:{0} End Date:{1}' .format(self.start_date, self.end_date)

1 个答案:

答案 0 :(得分:0)

问题,您可以将日期设置为购买日期,然后在日期范围内对其进行过滤

class PurchaseReport(models.Model):
    product_name = models.CharField(max_length=100, blank=False)
    quantity = models.IntegerField()
    price = models.DecimalField(max_digits=12, decimal_places=2)
    date = models.DateTimeField(null=True)    

然后可以制作一个def make_purchase

#just hook the date today to the purchase when create new 
def make_purchase(request):
    purchase = Purchase.object.create(
        product_name = request['product_name'],
        quantity = request['quantity'],
        price = request['price'],
        date = date.today()
    )
    purchase.save()

def display_purchase_report(request):
        report  = Purchases.objects.filter(date__range=["2018-10-01", "2011-11-1"])
        context = {'reports':report}
        return render(request, 'index3.html', context)

然后在模板中执行以下操作:

{% for record in records %}
    {{ record.product_name }}
    {{ record.quantity }}
{% endfor %}