Django:如何在自定义Django管理任务中更新ManyToManyField



在Django中,我有一个模型,其中一个字段(tags(是ManyToManyField:

class MyModel(models.Model):
id = models.CharField(max_length=30, primary_key=True)
title = models.CharField(max_length=300, default='')
author = models.CharField(max_length=300)
copy = models.TextField(blank=True, default='')
tags = models.ManyToManyField(Tag)

下面我有一个自定义管理任务,我在其中获取服务器上MyModel表的所有实例,并用它来替换本地服务器上的MyModel表的实例。(我正在本地运行此自定义管理任务(

以下是我目前正在尝试的内容。

然而,我得到了这个错误:CommandError:禁止直接分配到多对多集合的前端。请改用tag.set((

如何更新作为ManyToMany字段的tags字段?

from bytes.models import MyModel
class Command(BaseCommand):
def handle(self, *args, **options):
try:
// get queryset of MyModel instances from server
queryset = MyModel.objects.using('theserver').all()
// loop through that queryset and create a list of dictionaries
mymodel_list = [
{
'id': mymodel['id'],
'title': mymodel['title'],
'author': mymodel['author'],
'copy': mymodel['copy'],
'tags': mymodel['tags']
} 
for mymodel in queryset
]
//delete all objects in the local MyModel table
MyModel.objects.all().delete() 
//replace with MyModel instances from server
new_mymodel = [
MyModel(
id=mymodel['id'],
title=mymodel['title'],
author=mymodel['author'],
copy=mymodel['copy'],
tags=mymodel['tags'] <--- causing error
)
for mymodel in mymodel_list
]
MyModel.objects.bulk_create(new_mymodel)

ManyToMany字段的工作方式与其他字段不同。您不能简单地复制它的值,尤其是从字符串中复制。您可以在创建MyModel:之后尝试在循环中添加对象

//replace with MyModel instances from server
new_mymodels = []
for mymodel in mymodel_list:
new_mymodel = MyModel(
id=mymodel['id'],
title=mymodel['title'],
author=mymodel['author'],
copy=mymodel['copy']
)
for tag in mymodel['tags']:
new_mymodel.tags.add(tag)
new_mymodels.append(new_mymodel)
MyModel.objects.bulk_create(new_mymodel)

有可能,在.add()方法之前,您必须首先找到Tag对象。它取决于mymodel['tags']中的值。

相关内容

  • 没有找到相关文章

最新更新