模型方法的日期筛选器



大家好,我正在使用python和Django。

我目前有两个模型,我在其中编写模型方法来显示某些链接字段(FOREIGN KEYS(我将在下面展示这一点,现在我试图做的是能够按日期过滤这些模型方法,从我尝试的结果来看,我得到了一个错误,没有内置方法的签名。所以基本上我的模板呈现了这些模型方法,现在我想在日期范围之间搜索。

下面的代码是我的模型和方法,以及我试图过滤这些方法的函数。我们将非常感谢您的反馈。


#Sales agent model in models.py
class SalesAgent(models.Model):
user = models.OneToOneField(User,on_delete=models.SET_NULL,null=True,related_name="sauser")
Team_leader = models.ForeignKey(TeamLeader,on_delete=models.SET_NULL,null=True,related_name="team_agent")
SA_name = models.CharField(max_length=40)
role = models.CharField(choices=Roles,default="default",max_length=13)
def __str__(self):
return self.user.username
def sget_totals(self):
today = datetime.date.today()
return self.agent_sale.filter(Date_created__year=today.year,Date_created__month=today.month)
def sget_confirmed(self):
today = datetime.date.today()
return self.agent_sale.filter(State="Confirmed",Date_created__year=today.year,Date_created__month=today.month)
def sget_debi(self):
today = datetime.date.today()
return self.agent_sale.filter(AcknowledgeQA=True,State="Confirmed",Debi_status="Accepted",Date_created__year=today.year,Date_created__month=today.month)
def sget_requested(self):
today = datetime.date.today()
return self.agent_sale.filter(Debi_status="Requested",Date_created__year=today.year,Date_created__month=today.month)
def sget_cancelled(self):
today = datetime.date.today()
return self.agent_sale.filter(State="Cancelled",Date_created__year=today.year,Date_created__month=today.month)
def sget_pending(self):
today = datetime.date.today()
return self.agent_sale.filter(State="Pending",Date_created__year=today.year,Date_created__month=today.month)
#FILTERED SEARCH
def sfget_totals(self):
return self.agent_sale.all()
def sfget_confirmed(self):
return self.agent_sale.filter(State="Confirmed")
def sfget_debi(self):
return self.agent_sale.filter(AcknowledgeQA=True,State="Confirmed",Debi_status="Accepted")
def sfget_requested(self):
return self.agent_sale.filter(Debi_status="Requested")
def sfget_cancelled(self):
return self.agent_sale.filter(State="Cancelled")
def sfget_pending(self):
return self.agent_sale.filter(State="Pending")

#THIS IS THE FUNCTION IM TRYING in views.py
def asearch_stats(request):
if request.method == "POST":
from_date = request.POST['from']
to_date = request.POST['to']
flookup = SalesAgent.objects.filter(Q(agent_sale__Date_created__range=[from_date,to_date])).distinct
return render(request,"Sales/salesfilter.html",{"from_date":from_date,'to_date':to_date,"flookup":flookup})
else:
return render(request,"Sales/index.html",{})

您可能想使用自定义Manager,这样的东西应该可以工作:

型号.py

from .managers import SalesAgentManager
class SalesAgent(models.Model):
user = models.OneToOneField(...)
role = models.CharField(...)
...
manager = SalesAgentManager

经理.py

class SalesAgentQuerySet(models.QuerySet): 
def seller_roles(self):
return self.filter(role='Seller')
def superusers(self):
return self.filter(user__is_superuser=True)

class SalesAgentManager(models.Manager):

def get_queryset(self):
return SalesAgentQuerySet(self.model, using=self._db)

然后,你可以做:

# get all superuser sales agents:
superuser_sales_agents = SalesAgenet.objects.all().superusers()
# get all seller roles:
seller_roles = SalesAgent.objects.all().seller_roles()

相关内容

  • 没有找到相关文章

最新更新