tastypie保存\u m2m,TagRelatedManager评估为Fals

2024-05-13 13:08:39 发布

您现在位置:Python中文网/ 问答频道 /正文

实际上,我试图保存一个tastypie模型资源,它包含来自django-tagulousTagField

以下是我的模型和资源:

# 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调用之前,标记对象存在(print)捆绑数据['tags']),然后在super(SimpleModelResource, self).save_m2m(bundle)tastype中查找与tags字段关联的相关管理器,返回相关的\u mngr,但是当使用if not related_mngr对其求值时,它求值为跳过m2m关联。你知道吗


Tags: py模型titlemodelstags资源metaclass