小编vik*_*rma的帖子

如何计算django中两个日期之间的差异

我想计算当前日期和上一个日期之间的天数差异。我正在尝试这个代码

requiremntObj = CustomerLeads.objects.all()
a = datetime.datetime.now().date()

for i in requiremntObj:
    date1=i.posting_date
    diff = a-date1
    print diff
Run Code Online (Sandbox Code Playgroud)

我有一个错误 unsupported operand type(s) for -: 'datetime.date' and 'unicode'

对于当前日期,我正在获取 datetime 对象,对于 date1,我正在获取 unicode。

posting_date = models.DateField()
Run Code Online (Sandbox Code Playgroud)

python django

6
推荐指数
1
解决办法
2万
查看次数

在django中自动删除超过10天的数据

在我的django项目中,我想自动删除超过10天的数据.

我怎么写这个观点?它如何自动执行?

在我的模型中有一个post_date字段,我想通过该字段检查它是否是10天.

model.py:

class CustomerLeads(models.Model):
title = models.CharField(max_length=100, null=True, blank=True)
budget = models.IntegerField(default=0,null=True, blank=True)
posting_date = models.CharField(max_length=300,null=True, blank=True)
quantity = models.IntegerField(default=1,null=True, blank=True)
Run Code Online (Sandbox Code Playgroud)

我怎么能得到差异天.我2016-12-15 <type 'datetime.date'>从当前日期开始,并发布我的日期值12-Dec-2016 <type 'unicode'>

提前致谢.

python django

5
推荐指数
1
解决办法
2353
查看次数

从两个列表制作字典

我有两个清单

l1 = ['cat','dog']
l2= [1,2]
Run Code Online (Sandbox Code Playgroud)

现在我想制作一个这样的字典:

dict { {'name':cat,'id'=1}{'name':dog,'id'=2}}
Run Code Online (Sandbox Code Playgroud)

我正在使用,zip但这不符合我的要求.

python django

1
推荐指数
1
解决办法
64
查看次数

如何在sql查询中应用if else

我已经编写了一个sql查询来获取产品现在我想在我的查询中应用where子句但是我在哪里有条件

if stock = 'in':
 where will be num>0:

if stock = 'out':
where will be num = 0;
 if stock = ' ':
no where clause will apply
Run Code Online (Sandbox Code Playgroud)

我写这个有两个条件

where       
   case 
      when stock='in' then num_in_stock > 0
      when stock ='out' then num_in_stock = 0
   end;
Run Code Online (Sandbox Code Playgroud)

但它不起作用.我收到一个错误:

(1064,"您的SQL语法有错误;请查看与您的MySQL服务器版本对应的手册,以获得正确的语法,以便在'第20行使用a.date_updat'的a.partner_id = 5顺序" )我得到的股票变量

stock = request.GET.get('stock')
Run Code Online (Sandbox Code Playgroud)

我的完整查询是

 SELECT distinct a.product_id, a.variant_id 
    from 
    (
        SELECT cp.id as variant_id,
        COALESCE(cp.parent_id,cp.id) as product_id,
        ps.partner_id as partner_id,
        ps.price_retail as price_retail,
        COALESCE(oc.order_count,0) as order_count,
        cp.date_updated as date_updated …
Run Code Online (Sandbox Code Playgroud)

mysql

1
推荐指数
1
解决办法
55
查看次数

标签 统计

django ×3

python ×3

mysql ×1