我想知道条目是否在过去 6 个月内更新过。
这是我尝试过的:
def is_old(self):
"""
Is older than 6 months (since last update)
"""
time_threshold = datetime.date.today() - datetime.timedelta(6*365/12)
if self.last_update < time_threshold:
return False
return True
Run Code Online (Sandbox Code Playgroud)
但我收到错误:
if self.last_update < time_threshold:
TypeError: can't compare datetime.datetime to datetime.date
Run Code Online (Sandbox Code Playgroud) 我有一个类方法来创建字典。我像这样设置并获取这些值:
class Test(object):
def __init__(self):
self.config = {}
def set_config_key(self, key, value):
self.config[key] = value
def _get_config_value(self, key):
return self.config[key]
Run Code Online (Sandbox Code Playgroud)
在 Python 2.7 中有没有更好的方法来做到这一点?
在AngularJS中使用拦截器,当任何请求完成时,如何 console.log("finished AJAX request") ?
我一直在研究拦截器,到目前为止有以下内容,但它在请求开始时而不是结束时触发。
app.factory('myInterceptor', [function() {
console.log("finished AJAX request")
var myInterceptor = {
};
return myInterceptor;
}]);
Run Code Online (Sandbox Code Playgroud)
配置:
app.config(function ($stateProvider, $urlRouterProvider, $httpProvider) {
$httpProvider.interceptors.push('myInterceptor');
etc
Run Code Online (Sandbox Code Playgroud) 在Test2类中,我想在其类Meta中包含模型和字段.这可能吗?怎么样?这是我试过的......
我有一个混合:
class Test1(object):
pass
class Meta:
fields = ("url",)
class Test2(Test1):
pass
class Meta:
super(Meta) <=== does not work
models= test
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用以下方法为我的一个模型创建一个缩短的ID:
_char_map = string.ascii_letters+string.digits
def index_to_char(sequence):
return "".join([_char_map[x] for x in sequence])
def make_short_id(self):
_id = self.id
digits = []
while _id > 0:
rem = _id % 62
digits.append(rem)
_id /= 62
digits.reverse()
return index_to_char(digits)
@staticmethod
def decode_id(string):
i = 0
for c in string:
i = i * 64 + _char_map.index(c)
return i
Run Code Online (Sandbox Code Playgroud)
self.iduuid即在哪里1c7a2bc6-ca2d-47ab-9808-1820241cf4d4,但出现以下错误:
rem = _id%62 TypeError:在字符串格式化期间并非所有参数都已转换
仅当id是时,此方法才有效int。
如何修改缩短uuuid和解码的方法?
更新:
感谢您的帮助。我试图找到一种创建编码和解码方法的方法,该方法采用一个字符串,使其变短,然后再次将其解码。上面指出的方法永远无法使用字符串(uuid),
我需要在我的URL中允许ID匹配
[int]_接着长度22的随机字符串可以是字母或数字,但从来没有具有这种URL不安全字符串作为/或+或=等例如一个id看起来像这样:
即
这就是Django Docs对标准id匹配的看法......
url(r'^article/(?P<pk>[0-9]+)/?$', views.detail.as_view(), name='article'),
Run Code Online (Sandbox Code Playgroud)
如何匹配/验证我的ID类型与上面的正则表达式?
我已经尝试了[\w|\W]+等,但这不验证也不匹配我的用例
我有一个奇怪的问题,生成相同的PK给我错误:
django.db.utils.IntegrityError: duplicate key value violates unique constraint "Comment_pkey"
DETAIL: Key (id)=(uxlt72XrRu-fm260qHo9Zg) already exists.
Run Code Online (Sandbox Code Playgroud)
这是我的模特:
class Comment(models.Model):
id = models.CharField(primary_key=True, max_length=28, unique=True,
default="make_id()", editable=False)
description = models.TextField(max_length=255)
Run Code Online (Sandbox Code Playgroud)
生成ID的功能:
def make_id():
return base64.b64encode(uuid.uuid4().bytes).decode("utf-8")
Run Code Online (Sandbox Code Playgroud)
我是如何得到错误的:
c = Comment.objects.create(description ="test")<====有效
c2 = Comment.objects.create(description ="test2")<===违反了唯一约束
那么为什么我的模型每次都不会生成新的ID?同样的事情发生在测试而不仅仅是shell.
Django Rest框架显示了如何测试响应当你想测试整个返回的json时,这是可以的.但是,如果我只是想测试响应是否包含特定的键和值,我试过这个...
def test_get_user_shows_count(self):
url = reverse('user_list')
response = self.api_factory.get(url)
self.assertContains(response, {'count': 1})
Run Code Online (Sandbox Code Playgroud)
JSON响应
{
'count': 1,
'is_active': False,
'url': 'http: //testserver/v1/user/95',
'id': 95,
'display_name': None
}
Run Code Online (Sandbox Code Playgroud)
然而,这失败了,我能让它工作的唯一方法是将完整的例外json放入其中,我不想这样做.我如何测试上面的包含count并且只是1的eq?
我有以下功能:
epoch = datetime(1970, 1, 1)
def epoch_seconds(date):
"""Returns the number of seconds from the epoch to date."""
print(epoch)
td = date - epoch
return td.days * 86400 + td.seconds + (float(td.microseconds) / 1000000)
Run Code Online (Sandbox Code Playgroud)
当我直接从模型中获取日期时,出现以下错误:
打印(epoch_seconds(自我修改))
不能减去偏移天真和偏移感知日期时间
所以我检查了两者的格式self.modified和epoch
self.modified = 2015-08-13 16:29:37.601681+00:00
epoch = 1970-01-01 00:00:00
Run Code Online (Sandbox Code Playgroud)
我想我理解错误并需要这些格式相同,但是,我不知道是什么.601681+00:00以及如何执行此操作。是否可以帮助解释在之后是什么.以及如何使这些匹配?
如何向echo用户输出docker-machine ip default在Makefile 中运行的结果?
我尝试过以下方法:
display:
$(shell echo $(docker-machine env default))
Run Code Online (Sandbox Code Playgroud)
这只是打印: make: `display' is up to date.
python ×8
django ×6
python-2.7 ×2
angularjs ×1
docker ×1
javascript ×1
linux ×1
makefile ×1
regex ×1