简体   繁体   English

从Django中的ManyToMany关系中删除queryset

[英]Removing queryset from ManyToMany relationship in Django

I have models Amenity and Property shown below 我有如下所示的设施和属性模型

class Amenity(models.Model):
     feature= models.CharField(max_length=500, verbose_name='feature')


class Property(models.Model):
     name = models.CharField(max_length = 500,verbose_name='Property name')
     amenities = models.ManyToManyField(Amenity, null=True, blank=True)

How I remove queryset relationship without affecting the related models? 如何在不影响相关模型的情况下删除查询集关系?

How remove relation of all amenities of property object without deleting amenity objects? 如何在不删除便利对象的情况下删除财产对象所有便利设施的关系?

I tried 我试过了

for a in property.amenities.all():
   property.amenities.remove(a)

Thanks in advance 提前致谢

property.amenties.clear() is what you want. property.amenties.clear()是您想要的。

see: https://docs.djangoproject.com/en/dev/topics/db/examples/many_to_many/ 参见: https : //docs.djangoproject.com/en/dev/topics/db/examples/many_to_many/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM