【发布时间】:2021-06-22 08:04:11
【问题描述】:
我正在使用 Django 3.2
我有两个看起来有点像这样的模型:
class A(models.Model):
title = models.CharField(max_length=255)
caption = models.CharField(max_length=64)
image = models.ImageField()
# ... other fields unique to this model only
class B(models.Model):
title = models.CharField(max_length=255)
caption = models.CharField(max_length=64)
image = models.ImageField()
# ... other fields unique to this model only
我希望能够写出这样的函数:
def get_modelA_records_with_randomly_inserted_modelB(criteria_for_A, criteria_for_B, page_num):
qs_A = get_records_for_modelA(criteria_for_A, page_num)
qs_B = get_records_for_modelB(criteria_for_B, page_num)
# Randomly insert records of B into list of records of A
new_qs = qs_A | qs_B # how?
一个简单的方法是费力地遍历qs_A,根据随机布尔标志插入qs_B元素-但我想知道是否有更pythonic或“djangoistic”的方法来做到这一点?
【问题讨论】:
标签: python django django-queryset