Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 循环/更改对象列表中某些对象的最佳方法_Python_Django - Fatal编程技术网

Python 循环/更改对象列表中某些对象的最佳方法

Python 循环/更改对象列表中某些对象的最佳方法,python,django,Python,Django,假设我有这个代码: # Get 30 threads threads = Thread.objects.all()[:30] threads_id = [o.pk for o in threads] # Extra info about threads that the user have visited visited_threads = VisitedThread.objects.filter(pk__in=threads_id, user=request.user) # I want t

假设我有这个代码:

# Get 30 threads
threads = Thread.objects.all()[:30]
threads_id = [o.pk for o in threads]
# Extra info about threads that the user have visited
visited_threads = VisitedThread.objects.filter(pk__in=threads_id, user=request.user)

# I want to loop the visited_threads and add info to thread in threads with new info
for visited_thread in visited_threads:
    # Here I want to add things to thread (visited_thread.thread), something like:
    # thread.has_unread_post = thread.post_count > visited_thread.post_count

如何向线程列表中的线程添加信息,如代码示例中的内容?我不想更新数据库,只需在向用户显示数据之前对其进行操作。

您显示的示例代码很好,至少在一般意义上是这样。一旦开始迭代queryset,Django将创建内存中的模型实例,并且可以像任何其他Python对象一样向内存中的版本添加属性

要能够基于第二个qs编辑第一个qs中的线程,请执行以下操作:

threads = Thread.objects.all()[:30]
threads_by_pk = dict((t.pk, t) for t in threads)
# Extra info about threads that the user have visited
visited_threads = VisitedThread.objects.filter(pk__in=threads_by_pk.keys(), user=request.user)

# I want to loop the visited_threads and add info to thread in threads with new info
for visited_thread in visited_threads:
    thread = threads_by_pk[visited_thread.pk]
    thread.has_unread_post = thread.post_count > visited_thread.post_count

您展示的示例代码很好,至少在一般意义上是这样。一旦开始迭代queryset,Django将创建内存中的模型实例,并且可以像任何其他Python对象一样向内存中的版本添加属性

要能够基于第二个qs编辑第一个qs中的线程,请执行以下操作:

threads = Thread.objects.all()[:30]
threads_by_pk = dict((t.pk, t) for t in threads)
# Extra info about threads that the user have visited
visited_threads = VisitedThread.objects.filter(pk__in=threads_by_pk.keys(), user=request.user)

# I want to loop the visited_threads and add info to thread in threads with new info
for visited_thread in visited_threads:
    thread = threads_by_pk[visited_thread.pk]
    thread.has_unread_post = thread.post_count > visited_thread.post_count

您展示的示例代码很好,至少在一般意义上是这样。一旦开始迭代queryset,Django将创建内存中的模型实例,并且可以像任何其他Python对象一样向内存中的版本添加属性

要能够基于第二个qs编辑第一个qs中的线程,请执行以下操作:

threads = Thread.objects.all()[:30]
threads_by_pk = dict((t.pk, t) for t in threads)
# Extra info about threads that the user have visited
visited_threads = VisitedThread.objects.filter(pk__in=threads_by_pk.keys(), user=request.user)

# I want to loop the visited_threads and add info to thread in threads with new info
for visited_thread in visited_threads:
    thread = threads_by_pk[visited_thread.pk]
    thread.has_unread_post = thread.post_count > visited_thread.post_count

您展示的示例代码很好,至少在一般意义上是这样。一旦开始迭代queryset,Django将创建内存中的模型实例,并且可以像任何其他Python对象一样向内存中的版本添加属性

要能够基于第二个qs编辑第一个qs中的线程,请执行以下操作:

threads = Thread.objects.all()[:30]
threads_by_pk = dict((t.pk, t) for t in threads)
# Extra info about threads that the user have visited
visited_threads = VisitedThread.objects.filter(pk__in=threads_by_pk.keys(), user=request.user)

# I want to loop the visited_threads and add info to thread in threads with new info
for visited_thread in visited_threads:
    thread = threads_by_pk[visited_thread.pk]
    thread.has_unread_post = thread.post_count > visited_thread.post_count

是的,但是如何在线程中为线程添加属性呢?假设我访问了\u线程,访问的\u thread.thread.id=12。如何在threads中向thread.id=12添加属性?我将从threads queryset构建一个PKs到线程实例的字典,然后您可以在循环中查找内存中的线程实例以添加属性。添加了一个示例。似乎有点怀疑您的两个线程模型似乎共享pks,但这就是问题所在。感谢您提供的示例!不,他们不共享pk,你的线程有一个外键到线程pk。是的,但是我如何在线程中为线程添加属性?假设我访问了\u线程,访问的\u thread.thread.id=12。如何在threads中向thread.id=12添加属性?我将从threads queryset构建一个PKs到线程实例的字典,然后您可以在循环中查找内存中的线程实例以添加属性。添加了一个示例。似乎有点怀疑您的两个线程模型似乎共享pks,但这就是问题所在。感谢您提供的示例!不,他们不共享pk,你的线程有一个外键到线程pk。是的,但是我如何在线程中为线程添加属性?假设我访问了\u线程,访问的\u thread.thread.id=12。如何在threads中向thread.id=12添加属性?我将从threads queryset构建一个PKs到线程实例的字典,然后您可以在循环中查找内存中的线程实例以添加属性。添加了一个示例。似乎有点怀疑您的两个线程模型似乎共享pks,但这就是问题所在。感谢您提供的示例!不,他们不共享pk,你的线程有一个外键到线程pk。是的,但是我如何在线程中为线程添加属性?假设我访问了\u线程,访问的\u thread.thread.id=12。如何在threads中向thread.id=12添加属性?我将从threads queryset构建一个PKs到线程实例的字典,然后您可以在循环中查找内存中的线程实例以添加属性。添加了一个示例。似乎有点怀疑您的两个线程模型似乎共享pks,但这就是问题所在。感谢您提供的示例!不,他们不共享pk,因为线程pk有一个外键。