我在settings.pydjango项目的文件中设置了时区:
TIME_ZONE = 'US/Eastern'
现在我的日志包含美国/东部时间.
我想在我的日志中保留UTC时间.那可能吗?
我正在使用 Django 2.1.1。
我有一个模型Analysis,其中包含一个MyFile模型的外键(我编写的用于处理文件的模型):
from polymorphic.models import PolymorphicModel
from django.db.models import Model, DateTimeField, FileField, SET_NULL
from django.db.models.signals import pre_delete
class MyFile(Model):
file = FileField(upload_to='./', null=False, blank=False)
description = CharField(max_length=255, null=True, blank=True)
date_added = DateTimeField(auto_now_add=True)
@receiver(pre_delete, sender=MyFile)
def mymodel_delete(sender, instance, **kwargs):
"""
To delete the file connected to the `sender` class: receive the pre_delete signal
and delete the file associated with the model instance.
"""
instance.file.delete(False)
class Analysis(PolymorphicModel):
# ... other fields ...
file_results = ForeignKey(MyFile, on_delete=SET_NULL, …Run Code Online (Sandbox Code Playgroud) 我们正在解决我们的一个网站上的几个性能问题,我们注意到命令“SET TIME ZONE 'America/Chicago'”被执行得如此频繁,在 24 小时内,不到 1 小时(或大约 4% 的总 DB CPU 资源)用于运行该命令。
请注意,“USE_TZ”设置为 False,因此根据我的理解,所有内容都应以 UTC 格式存储在数据库中,并且仅在必要时在 UI 中转换为本地时区。
您对我们如何消除数据库服务器上的这种压力有什么想法吗?
我对 elm 还很陌生,并且遇到了使用后端数据填充模型的问题。我目前可以向服务器发出 get 请求,该请求返回一个 byte[] (数据是任何类型的图像、音频或视频),当仅通过 Html.img 显示此数据时,它可以正常工作。当我尝试使用 Http.get (src: https: //package.elm-lang.org/packages/elm/http/latest/Http)来填充我的模型时,它需要解码器。问题是,Bytes.Decode.bytes 需要一个 Int 来知道需要解码多少字节。所以我的问题是:有没有什么方法可以访问字节宽度,同时仍然匹配 Http.get 的类型模式?
这是我的问题的一个简单示例:
import Bytes exposing (Bytes)
import Bytes.Decode exposing (Decoder, bytes, decode)
import GeneralTypes exposing (Msg(..))
import Http
getMediaFromUrl : Cmd Msg
getMediaFromUrl = Http.get
{ url = "http://localhost:8090/image/2006/aa@a.de/session"
, expect = Http.expectBytes GetThumbnail decodeBytes
}
decodeBytes: Bytes.Bytes -> Decoder Bytes
decodeBytes bytesToDecode= let
fileSize =
bytesToDecode |> Bytes.width
in
Bytes.Decode.bytes fileSize
Run Code Online (Sandbox Code Playgroud)
module GeneralTypes exposing (..)
import Bytes exposing (Bytes)
import Http …Run Code Online (Sandbox Code Playgroud) 我用这个命令创建了virtualenv virtualenv blog.但它只有python 2.7.假设我想在python3中编写django代码,我应该安装python3吗?因为当我运行python manage.py shellpython2时会出现提示.如果我应该,我该怎么办?在我的真实系统中,python 2和3都会出现.
如何在具有相反关系的方法中使用 admin_order_field?
是否可以使用一些聚合查询集功能?
class Card(models.Model):
serial_number = models.CharField(max_length=200)
...
class License(models.Model):
expiration = models.DateTimeField('Expiration')
card = models.ForeignKey('Card')
...
class Pc(models.Model):
card = models.ForeignKey('Card')
...
class PcAdmin(admin.ModelAdmin):
...
def expiration(self, obj):
try:
license = License.objects.get(card=obj.card, type="xxx")
expiration = license.expiration
except (License.DoesNotExist, TypeError):
expiration = "N/A"
return expiration
expiration.short_description = _("Expiration")
expiration.admin_order_field = ???
Run Code Online (Sandbox Code Playgroud) 如何根据模型字段的当前值更新模型字段并避免出现竞争状况?更新任务可以写为:
if (self.x == y):
self.x = z
self.save()
else:
raise Exception()
Run Code Online (Sandbox Code Playgroud)
但是,存在竞争条件。我想出了以下解决方案:
from django.db import transaction
with transaction.atomic():
if (self.x == y):
self.x = z
self.save()
else:
raise Exception()
Run Code Online (Sandbox Code Playgroud)
但这安全吗,还有更好的方法吗?
我的计划是将一些现有的 Redis 键存储在哈希中,稍后从 Redis Lua 脚本中获取并执行操作。我读到,最佳实践是在调用时提供脚本中使用的所有键EVAL。
我的问题是,运行一个在运行时没有提供任何密钥但对从脚本中EVAL获取的某些密钥进行操作的脚本是否安全?如果不这样做,会产生什么后果?如何避免这个缺点?
我提到,在 时EVAL,无法知道该特定哈希中的键是什么。我可以在 之前的步骤中从哈希中获取所有密钥EVAL,然后将它们提供给EVAL,但这听起来有点矫枉过正。
我希望在我的 Django 数据库中创建记录时自动记录当前时间。
在我的模型中,我使用:
dateTime = models.DateTimeField(auto_now_add=True)
Run Code Online (Sandbox Code Playgroud)
根据我的理解,考虑到正确的时区,这会自动标记时间。然而,这是输出比我的当地时间早 5 小时的时间。
所以为了尝试调试这个,我在我的视图中运行了这个函数并打印了输出:
from django.utils import timezone
timeNow = timezone.localtime(timezone.now())
Run Code Online (Sandbox Code Playgroud)
timeNow 将输出正确的时间。
所以我将模型更改为:
def get_time():
return timezone.localtime(timezone.now())
dateTime = models.DateTimeField(default = get_time)
Run Code Online (Sandbox Code Playgroud)
这仍然会导致相同的错误时间戳。
我还在 settings.py 中更改了时区
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'Canada/Central'
USE_I18N = True
USE_L10N = True
USE_TZ = True
Run Code Online (Sandbox Code Playgroud)
任何想法我缺少什么?
我有一些不可变的图像资源,可以永久缓存。Chrome 似乎尊重我的响应标头,并且不会重新验证资源:
以下是 Chrome 中这些资源之一的示例。正如你所看到的,我包括cache-control: public, max-age,expires,etag和last-modified和资源从“内存缓存”服务:
但是,Firefox 不尊重这些标头并在每次加载时重新验证资源!每次页面加载时,我的服务器都会收到对每个个人资料图片的请求,并返回 304:
这是导致 304 的此类请求的示例:
我不明白为什么 Firefox 会忽略缓存标头并继续访问 304 的服务器。我已经尝试了各种与缓存相关的标头并阅读了关于什么是"cacheable"的标准。我确保在 devtools 中启用了缓存。我也尝试过关闭 devtools,但我一直在服务器日志中看到 304。
我发现这只会在页面刷新时发生。但是,普通刷新,不是 shift- 或 shift-command-,而只是普通刷新。这不是我期望的行为。
django ×7
python ×3
browser ×1
byte ×1
caching ×1
decoding ×1
django-admin ×1
elm ×1
firefox ×1
http ×1
http-headers ×1
lua ×1
postgresql ×1
redis ×1
timezone ×1
transactions ×1
virtualenv ×1