为Django中的Model实例更新M2M关系



我有三种型号,如下所示:

class Service(models.Model):
name = models.CharField(max_length=50, unique=True)
class ServiceGroup(models.Model):
name = models.CharField(max_length=50, unique=True)
services = models.ManyToManyField(Service, through=ServiceToServiceGroup)
class ServiceToServiceGroup(models.Model):
service = models.ForeignKey(Service)
service_group = models.ForeignKey(ServiceGroup)

我有一个服务列表,我需要将其作为ServiceGroup实例的M2M关联。我尝试了以下方法:

In [145]: instance = ServiceGroup.objects.get(pk=1)                                                                                                                                                                                                                      
In [146]: instance.services.all()                                                                                                                                                                                                                                        
Out[146]: <QuerySet [<Service: service-1>]>
In [147]: new = ['service-2']                                                                                                                                                                                                                         
In [148]: service_objects = Service.objects.filter(name__in=new)                                                                                                                                                                                    
In [149]: service_objects                                                                                                                                                                                                                                                
Out[149]: <QuerySet [<Service: service-2>]>
In [150]: instance.service_set = service_objects                                                                                                                                                                                                                         
In [151]: instance.save()                                                                                                                                                                                                                                                
In [152]: instance.services.all()                                                                                                                                                                                                                                        
Out[152]: <QuerySet [<Service: service-1>]>

然而,显而易见的是,这并没有奏效。正确的方法是什么?

为了创建连接,您需要创建一个中介实例:

for service in service_objects:
ServiceToServiceGroup.objects.create(
service=service,
service_group=instance,
)

也可以使用批量创建:

ServiceToServiceGroup.objects.bulk_create(
[ServiceToServiceGroup(
service=service, 
service_group=instance
) for service in service_objects]
)

对于删除旧关系,您可以运行:

ServiceToServiceGroup.objects.filter(service_group=instance).delete()

instance.servicetoservicegroup_set.all().delete()

在运行添加脚本之前。

相关内容

  • 没有找到相关文章

最新更新