Python Django自定义管理器,用于具有计算字段的配置文件
我想为用户配置文件添加一个计算字段。 我正在添加自定义管理器:Python Django自定义管理器,用于具有计算字段的配置文件,python,django,Python,Django,我想为用户配置文件添加一个计算字段。 我正在添加自定义管理器: class ProCalcManager(models.Manager): use_for_related_fields = True def get_queryset(self): result = super(ProCalcManager, self).get_queryset() result = result.extra( select={'pro': "(
class ProCalcManager(models.Manager):
use_for_related_fields = True
def get_queryset(self):
result = super(ProCalcManager, self).get_queryset()
result = result.extra(
select={'pro': "(start_date, INTERVAL '10 days') OVERLAPS"
"(CURRENT_TIMESTAMP, INTERVAL '1 days')"}
)
return result
class Profile (models.Model):
objects = ProCalcManager()
default_manager = ProCalcManager()
start_date = models.DateTimeField()
当我使用:Users.objects.first().profile.pro
结果为True
,但对于Users.objects.filter(profile\uu pro=True)
结果为{TypeError}相关字段的查找无效:pro
如何在用户配置文件中添加计算字段“pro”以在过滤器中使用它,我可以使用另一个更新查询集?您所做的只是计算日期,然后确定用户是否为
pro
。您可以使用simple.filter()
来实现它,而不必为计算和筛选编写sql语句
首先,不要重写get\u queryset()
方法,而是在模型中添加一个名为pro
的属性
@property
def pro(self):
calculated_end_date = tz.now() + tz.timedelta(days=-10)
return self.start_date > calculated_end_date
通过这样做,您将能够访问配置文件的pro
属性。就像你用额外的钱一样
>>> p = Profile.objects.first()
>>> p.pro
>>> True
然后,为了能够过滤配置文件的pro
值,您可以为than编写查询集,并将其用作管理器。大概是这样的:
class ProCalcQuerySet(models.QuerySet):
def is_pro(self, value=True):
calculated_end_date = tz.now() + tz.timedelta(days=-10)
if value:
lookup = 'gte'
else:
lookup = 'lt'
filter_dict = {
'start_date__' + lookup: calculated_end_date
}
return self.filter(**filter_dict)
In [2]: Profile.objects.is_pro(value=True)
Out[2]: [<Profile: Profile object>]
In [3]: Profile.objects.is_pro()
Out[3]: [<Profile: Profile object>]
In [4]: Profile.objects.is_pro(False)
Out[4]: []
In [8]: Profile.objects.is_pro().first().pro
Out[8]: True
然后将此查询集用作管理器,以便在模型对象中访问它
class Profile (models.Model):
objects = ProCalcQuerySet.as_manager()
default_manager = ProCalcQuerySet.as_manager()
然后你可以这样使用它:
class ProCalcQuerySet(models.QuerySet):
def is_pro(self, value=True):
calculated_end_date = tz.now() + tz.timedelta(days=-10)
if value:
lookup = 'gte'
else:
lookup = 'lt'
filter_dict = {
'start_date__' + lookup: calculated_end_date
}
return self.filter(**filter_dict)
In [2]: Profile.objects.is_pro(value=True)
Out[2]: [<Profile: Profile object>]
In [3]: Profile.objects.is_pro()
Out[3]: [<Profile: Profile object>]
In [4]: Profile.objects.is_pro(False)
Out[4]: []
In [8]: Profile.objects.is_pro().first().pro
Out[8]: True
[2]中的:Profile.objects.is_pro(value=True)
输出[2]:[]
[3]中:Profile.objects.is_pro()
输出[3]:[]
[4]中:Profile.objects.is_pro(False)
Out[4]:[]
[8]中:Profile.objects.is_pro().first().pro
Out[8]:对
您要做的就是计算日期,然后确定用户是否是pro
。您可以使用simple.filter()
来实现它,而不必为计算和筛选编写sql语句
首先,不要重写get\u queryset()
方法,而是在模型中添加一个名为pro
的属性
@property
def pro(self):
calculated_end_date = tz.now() + tz.timedelta(days=-10)
return self.start_date > calculated_end_date
通过这样做,您将能够访问配置文件的pro
属性。就像你用额外的钱一样
>>> p = Profile.objects.first()
>>> p.pro
>>> True
然后,为了能够过滤配置文件的pro
值,您可以为than编写查询集,并将其用作管理器。大概是这样的:
class ProCalcQuerySet(models.QuerySet):
def is_pro(self, value=True):
calculated_end_date = tz.now() + tz.timedelta(days=-10)
if value:
lookup = 'gte'
else:
lookup = 'lt'
filter_dict = {
'start_date__' + lookup: calculated_end_date
}
return self.filter(**filter_dict)
In [2]: Profile.objects.is_pro(value=True)
Out[2]: [<Profile: Profile object>]
In [3]: Profile.objects.is_pro()
Out[3]: [<Profile: Profile object>]
In [4]: Profile.objects.is_pro(False)
Out[4]: []
In [8]: Profile.objects.is_pro().first().pro
Out[8]: True
然后将此查询集用作管理器,以便在模型对象中访问它
class Profile (models.Model):
objects = ProCalcQuerySet.as_manager()
default_manager = ProCalcQuerySet.as_manager()
然后你可以这样使用它:
class ProCalcQuerySet(models.QuerySet):
def is_pro(self, value=True):
calculated_end_date = tz.now() + tz.timedelta(days=-10)
if value:
lookup = 'gte'
else:
lookup = 'lt'
filter_dict = {
'start_date__' + lookup: calculated_end_date
}
return self.filter(**filter_dict)
In [2]: Profile.objects.is_pro(value=True)
Out[2]: [<Profile: Profile object>]
In [3]: Profile.objects.is_pro()
Out[3]: [<Profile: Profile object>]
In [4]: Profile.objects.is_pro(False)
Out[4]: []
In [8]: Profile.objects.is_pro().first().pro
Out[8]: True
[2]中的:Profile.objects.is_pro(value=True)
输出[2]:[]
[3]中:Profile.objects.is_pro()
输出[3]:[]
[4]中:Profile.objects.is_pro(False)
Out[4]:[]
[8]中:Profile.objects.is_pro().first().pro
Out[8]:对
如果我错了,请纠正我,据我所知,pro
字段实际上是startdate+10天>当前日期,不需要使用额外的
,您也可以使用过滤器。是的,但此过滤器必须在系统中的许多位置使用,因此我想在模型的一个位置放置一个过滤器。如果我错了,请纠正我,据我所知,pro
字段实际上是startdate+10天>当前日期,不需要使用extra
,您也可以使用过滤器。是的,但此过滤器必须在系统中的许多位置使用,因此我想在模型的一个位置放置一个过滤器。