gpt4 book ai didi

python - 无法让 ToMany 在 Tastypie 中工作

转载 作者:太空狗 更新时间:2023-10-30 02:21:40 25 4
gpt4 key购买 nike

我正在关注 Tastypie 文档,但发现自己完全被困住了。我有以下内容:

应用程序接口(interface):

class ProjectResource(ModelResource):
milestones = fields.ToManyField('ProjectTrackerServer.projects.api.MilestoneResource', 'projects', related_name='project', full=True)

class Meta:
queryset = Project.objects.all()
resource_name = 'project'


class MilestoneResource(ModelResource):
project = fields.ToOneField('ProjectTrackerServer.projects.api.ProjectResource', 'project')

class Meta:
queryset = Milestone.objects.all()
resource_name = 'milestone'

[更新:以上 API 有效 - 基于以下模型]

这是我的模型。

模型 - 里程碑:

from django.db import models
from ProjectTrackerServer.projects.models import Project
class Milestone(models.Model):
project = models.ForeignKey(Project, related_name='projects')
name = models.TextField()
start_date = models.DateField()
due_date = models.DateField()
completed_date = models.DateField()
description = models.TextField()
status = models.IntegerField()

def __unicode__(self):
return self.name

模型 - 项目:

from django.db import models
from django.template.defaultfilters import slugify

class Project(models.Model):
name = models.CharField(max_length=200)
start_date = models.DateField()
end_date = models.DateField()
pm_id = models.IntegerField()
status = models.IntegerField()
slug = models.SlugField()

def __unicode__(self):
return self.name

def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.name)[:50]
return super(Project, self).save(*args, **kwargs)

我仍然得到同样的错误:

{"error_message": "'Project' 对象没有属性 'milestones'", "traceback": "Traceback (最近调用最后):\n\n 文件\"C:\Python27\lib\site- packages\tastypie\resources.py\",第 192 行,在包装器中resources.py\", line 406, in dispatch_detail\n return self.dispatch('detail', request, **kwargs)\n\n File\"C:\Python27\lib\site-packages\tastypie\resources. py\”,第 427 行,在 dispatch\n response = method(request, **kwargs)\n\n 文件\“C:\Python27\lib\site-packages\tastypie\resources.py\”,第 1058 行,在 get_detail\n bundle = self.full_dehydrate(bundle)\n\n 文件\"C:\Python27\lib\site-packages\tastypie\resources.py\",第 654 行,在 full_dehydrate\n bundle.data[field_name ] = field_object.dehydrate(bundle)\n\n 文件\"C:\Python27\lib\site-packages\tastypie\fields.py\",第 690 行,脱水\n the_m2ms = getattr(bundle.obj, self .attribute)\n\nAttributeError: '项目'对象没有属性“里程碑”\n"}

最佳答案

::其他资源::

This blog has an excellent django-tastypie reverse relationship example

当我遇到错误时 AttributeError: 'Options' object has no attribute 'api_name'\n"}

我将示例行输入为

 milestones = fields.ToManyField('ProjectTrackerServer.projects.api.Milestone', 'projects', full=True)

确保您列出的是“..api.Milestone资源

~还有~

如果您得到一个包含关系的空列表,请确保您传递给“.ToManyField”的第二个参数与您在模型文件中指定的相关名称相匹配。

milestones = fields.ToManyField('ProjectTrackerServer.projects.api.MilestoneResource', 'projects', full=True)

class Milestone(models.Model):
project = models.ForeignKey(Project, related_name='projects')
...

关于python - 无法让 ToMany 在 Tastypie 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14822280/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com