Django TastyPie Patch to a many-to-many [英] Django TastyPie Patch to a Many-to-Many

查看:273
本文介绍了Django TastyPie Patch to a many-to-many的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用TastyPie 补丁到一对多,但我收到这个错误:

I'm trying to use TastyPie Patch to a many-to-many, but I get this error:

error_message:Tastypie需要一个Python风格的路径()来延迟加载相关资源,只给定SchemeResource。

为什么?

我正在制作的补丁:

/participant/84
POST: {"email":"test@test.com",  "schemes":{"id":"12", "schemes"}}

资源:

class ParticipantResource(ModelResource):

    schemes = fields.ToManyField('SchemeResource', attribute='schemes', full=True, null=True)

    class Meta:
        queryset = Participant.objects.all()
        resource_name = 'participant'
        allowed_methods = ['post', 'get', 'put', 'patch']

第二资源

class SchemeResource(ModelResource):

    user = fields.ToOneField(UserResource, 'user', full=True)
    link = fields.ToOneField(SchemeLinkResource, 'link', full=True)

    class Meta:
        queryset = Scheme.objects.all()
        resource_name = 'scheme'

模型:

class Participant(models.Model):

    email = models.EmailField(unique=True)
    mobile = PhoneNumberField(null=True, blank=True)
    date_joined = models.DateTimeField(_('date joined'), default=timezone.now)

    schemes = models.ManyToManyField(Scheme)


推荐答案

您必须使用括号,例如

[] 

在发布到m2m字段时,您的方案项目(即使是单数)。

around your schemes items (even if singular) when posting to a m2m field.

请求将会如下所示:

{"email":"test@test.com",  "schemes":[{"id":"12", "schemes"}]}

当您想知道请求应如何,请在url / of / api / modelresource / schema /

When you want to know what a request should look like, make a GET request on url/of/api/modelresource/schema/

上发出GET请求如果我正确地记得(尽管你的请求中写了POST),PATCH请求必须有

If I recall correctly (and although you wrote "POST" in your request), PATCH request must have

{"objects": [...]}

包围正文。

编辑

以下是一些适用于我的例子:

Here's an example of what works for me :

资源:

class VATCertificateResource(ModelResource):
    class Meta:
        queryset = VATCertificate.objects.all()
        resource_name = 'vatcertificate'
        authorization = Authorization()

class InterventionResource(ModelResource):

    vatcertificates = fields.ToManyField('core.api.VATCertificateResource', 'vatcertificates',
                                     related_name='intervention', null=True, blank=True, full=True)

模型:

class VATCertificate(Document):
    intervention = models.ForeignKey(Intervention, related_name='vatcertificates', blank=True, null=True)

class Intervention(models.Model):
    pass

希望这有帮助,

问候,

这篇关于Django TastyPie Patch to a many-to-many的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆