实际上,我正在尝试保存一个美味的模型资源,其中包含TagField
来自django-tagulous
这是我的模型和资源:
# models.py
class SimpleModel(models.Model):
topics = tagulous.models.TagField()
title = models.CharField(max_length=100)
# resources.py
class TopicResource(ModelResource):
class Meta:
queryset = SimpleModel.topics.tag_model.objects.all()
authentication = Authentication()
authorization = Authorization()
class SimpleModelResource(ModelResource):
tags = fields.ToManyField(TopicResource, attribute='topics', full=True, null=True)
class Meta:
queryset = SimpleModel.objects.all()
authentication = Authentication()
authorization = Authorization()
卷曲部分:
curl --request POST --dump-header - --header 'Content-Type: application/json' --data '{"tags": ["/api/v1/topic/1/"], "title": "A bad dream"}' localhost:8000/api/v1/simplemodel/
shell 不会返回任何错误,但是标签与新对象没有关联。
通过覆盖ModelResource save_m2m method
,在super
调用标签对象存在之前(打印bundle.data ['tags']),然后在super(SimpleModelResource, self).save_m2m(bundle)
tastepie中查找与标签字段关联的相关管理器,返回related_mngr,但是当使用以下方法对其进行评估时:if not related_mngr
它评估为True
跳过 m2m 关联。