Django将多个项从DB查询复制到di

2024-06-28 14:40:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Django和dicts有意见。我只想得到符合如下刺痛的物品。但我不能让它工作。谢谢你的帮助。你知道吗

django_db_query = [{'time': '13:00 Uhr', 'titel': 'test1'}, {'time': '14:00 blah', 'titel': 'test2'}, {'time': '13:00 Uhr', 'titel': 'test3'},]
all_db_items = Django_db.objects.all()

only_13 = dict()
for item in all_db_items:
    if item.time is "13":
        only_13 += item

需要:数据结构和多个值从我的数据库在我的dict,但只与时间13:00 Uhr

for item in only_13:
    print item.titel

console
   test1
   test2

Tags: djangoinonlyfordbtimeitemsall
2条回答

假设你的djangdbmodel看起来像

def DjangoDbModel(models.Model):
    time = models.DateTimeField()
    title = models.CharField(max_length=256)

在这种情况下你需要做的就是 DjangoDbModels.objects.filter(time__hour=13)如果您只想拥有从第13小时开始的项目。例如,可以对日、年和月应用类似的过滤器。你知道吗

for item in all_db_items:
    if item['time'] == "13:00 Uhr":
        only_13.update(item)

is仅适用于相同的操作数

+不是为dict实现的,请改用update

更多资源: https://www.python-course.eu/dictionaries.php

相关问题 更多 >