我想将一个查询集相关对象与另一个查询集相关对象合并。一些示例代码来解释:
## Models
# sample models to illustrate problem
class PetShop(models.Model):
id = models.AutoField(primary_key=True)
shop_name = models.CharField(maxlength=255)
cats = models.ManyToManyField(Cat)
class Cat(models.Model):
id = models.AutoField(primary_key=True)
cat_name = models.CharField(maxlength=50, blank=True)
## View
def MergePetsInShop(request):
source_shop = PetShop.objects.get(pk=2)
destination_shop = PetShop.objects.get(pk=3)
#Somehow merge CATS from one shop to the other
result = merge(source_shop.cats,destination_shop.cats)
#save()
我怎样才能正确地做到这一点?
非常感谢。