我正在尝试将TastyPie Patch
用于多对多,但我收到此错误:
“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)
答案 0 :(得分:1)
您必须使用括号,例如
[]
在发布到m2m字段时,您的方案项目(即使是单数)。
请求看起来像:
{"email":"test@test.com", "schemes":[{"id":"12", "schemes"}]}
如果您想知道请求应该是什么样的,请在url / of / api / modelresource / schema /
上发出GET请求如果我没记错(尽管你在请求中写了“POST”),PATCH请求必须有
{"objects": [...]}
封闭身体。
编辑:
这是一个对我有用的例子:
资源:
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
希望这有帮助,
此致