Django Python。。。更新数据库中的多个元素

Django Python。。。更新数据库中的多个元素,python,django,Python,Django,AoA, 我知道这个问题很傻,但我被困在这里了 这是代码 nq = Notifications.objects.filter(userid__iexact=q) for string in nq: string.markType = "Read" results = Notifications.objects.filter(Q(userid__iexact=q)).order_by('-id') 但是markType未能设置为“读取”。。。为什么?您可能

AoA, 我知道这个问题很傻,但我被困在这里了

这是代码

nq = Notifications.objects.filter(userid__iexact=q)
    for string in nq:
            string.markType = "Read"
    results = Notifications.objects.filter(Q(userid__iexact=q)).order_by('-id')

但是markType未能设置为“读取”。。。为什么?

您可能想在更改对象后保存它:

nq = Notifications.objects.filter(userid__iexact=q)
for string in nq:
    string.markType = "Read"
    string.save()
results = Notifications.objects.filter(Q(userid__iexact=q)).order_by('-id')

您可能打算在更改对象后保存它:

nq = Notifications.objects.filter(userid__iexact=q)
for string in nq:
    string.markType = "Read"
    string.save()
results = Notifications.objects.filter(Q(userid__iexact=q)).order_by('-id')

除了您忘记保存对象之外,建议在这里使用方法,它将比您当前的方法更快。但是,如果您覆盖了
save
方法,则不要使用它:

Notifications.objects.filter(userid__iexact=q).update(markType = "Read")

除了您忘记保存对象之外,建议在这里使用方法,它将比您当前的方法更快。但是,如果您覆盖了
save
方法,则不要使用它:

Notifications.objects.filter(userid__iexact=q).update(markType = "Read")

你从未调用过
string.save()
。你从未调用过
string.save()
。我告诉过你这是个愚蠢的问题谢谢你的帮助:)我告诉过你这是个愚蠢的问题谢谢你的帮助:)