无法从django arrayfield中删除元素
我正在尝试从notes模型中的arrayfield中删除一个元素无法从django arrayfield中删除元素,django,django-queryset,Django,Django Queryset,我正在尝试从notes模型中的arrayfield中删除一个元素 obj=notes.objects.get(id=n_id,related_to__contains=[c_id]) obj.related_to.remove(c_id) obj.save() arrayfield与何处相关 related_to=ArrayField(models.IntegerField()) n_id是便笺的id,c_id是联系人的id。当我尝试这样做时,我得到了错误值错误:列表。删除(x):x不在列
obj=notes.objects.get(id=n_id,related_to__contains=[c_id])
obj.related_to.remove(c_id)
obj.save()
arrayfield与何处相关
related_to=ArrayField(models.IntegerField())
n_id是便笺的id,c_id是联系人的id。当我尝试这样做时,我得到了错误值错误:列表。删除(x):x不在列表中。我查过了,c_id在arrayfield中。有什么问题吗?因为它找到了对象,
c\u id
肯定在related\u to
字段中。我相信您会遇到此错误,因为c_id
不是int
类型,而是string
类型。应首先将其转换为int:
c_id = int(c_id)
obj=notes.objects.get(id=n_id,related_to__contains=[c_id])
....
哎呀。我的错。我没想过。现在效果很好。谢谢你指出这一点。