- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试创建一个具有 0 到无限评论的资源(观察)。我陷入了以下错误:
"error": "The model '<Observation: Observation object>' has an empty attribute 'comments' and doesn't allow a null value."
class Observation(ObsModel):
taxon_node = models.ForeignKey(TaxonNode, related_name = 'observation_taxon_node', null = True)
substrate = models.ForeignKey(TaxonNode, related_name = 'observation_substrate', null = True, blank=True)
source = models.CharField(max_length=255, blank=True)
sample = models.ForeignKey(Sample)
remarks = models.TextField(blank = True)
exact_time = models.DateTimeField(null=True)
individual_count = models.IntegerField(null = True)
is_verified = models.NullBooleanField(null = True)
verified_by = models.ForeignKey(User, null = True)
verified_time = models.DateTimeField('time verified', null = True)
class Meta():
app_label = 'observation'
class Comment(models.Model):
observation = models.ForeignKey(Observation)
comment = models.TextField()
created_time = models.DateTimeField('time created', auto_now_add=True, editable=False)
class Meta:
app_label = 'observation_moderate'
class ObservationResource2(ModelResource):
comments = fields.ToManyField('apps.api.CommentResource2', 'comments')
class Meta:
queryset = Observation.objects.filter(is_verified=False)
authentication = SessionAuthentication()
authorization = DjangoAuthorization()
resource_name = 'observation'
class CommentResource2(ModelResource):
observation = fields.ToOneField(ObservationResource2, 'observation')
class Meta:
queryset = Comment.objects.all()
resource_name = 'comments'
authentication = SessionAuthentication()
authorization = DjangoAuthorization()
最佳答案
您缺少观察模型上的“评论”属性,
要么添加
observation = models.ForeignKey(Observation, related_name="comments")
comments = fields.ToManyField('apps.api.CommentResource2', 'comment_set', null=True)
关于Django-tastypie 一对多关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15999188/
我想在 GET 响应中包含一些字段,并在 POST 确认响应中包含较小的字段子集。我必须在 alter_detail_data_to_serialize 中拥有大量 del bundle['field
我在 tastypie 中有一个简单模型的 ModelResource,它有一个 id 和一个名称。 XML 输出如下所示。但我想用我的模型名称代替“对象”。我似乎一直在为如何解决这个问题而苦苦挣扎—
我有一个 Tastypie ModelResource,它从常规 Django 模型获取其字段。我想将 Tastypie 资源上的某些字段设置为只读,即使它们在底层模型中是可写的。这可以通过简单的方式
我知道如何为 tastypie 资源设置身份验证/授权:通过资源 Meta 类中的设置。但是,如何验证/授权对顶级模式的访问? 例如,我可以对位于/api/v1/resource 的资源进行身份验证/
第一个型号: class Profile(models.Model): username = models.CharField( _('username'),
尝试通过 python requests 以及命令行 cURL 提交 PATCH 请求,我收到以下响应: >>> r = requests.patch(url) >>> r.text u'{"erro
我在通过 tastypie api 保存项目时遇到问题。 (POST 方法) 这是我的 api.py 代码。 from tastypie.resources import ModelResource,
我在玩重客户端应用程序。 想象一下我有这个模型: class Category(models.Model): name = models.CharField(max_length=30)
是否可以使用tastypie 在相关模型上包含字段? 根据我下面的模型:如果我将一个 VideoContent 和一个 TextContent 实例持久化到数据库,那么我可以从我的 Content 资
我希望向我的 Tastypie 驱动的 API 添加某种分析。我真的很喜欢用于常规网站的 Google Analytics,但显然它不适用于 API。您通常如何对 API 进行分析?是否有任何可用于
我正在创建UserResource和UserProfileResource。当我创建一个新用户时,我看到我的用户配置文件数据库表也正在更新。哪个好但是当我得到用户时,我得到一个错误,说: The mo
我有这个代码: #api model class VideoResource(ModelResource): class Meta: queryset = Video.obje
我正在为一个项目开发 API,我通过 OrderProducts 建立了 Order/Products 关系,如下所示: 在目录/models.py class Product(models.Mode
我设置了 Django Tastypie API。 我想在数据库中查询与名称匹配的对象数。 这在现有模型资源上是否可行,或者我是否需要设置新资源来处理这种特定情况? (这些数据通常在结果的元数据部分返
我试图让我的 api 给我与tastypie 的反向关系数据。 我有两个模型,DocumentContainer 和 DocumentEvent,它们的关系如下: DocumentContainer
我正在构建一个 django 美味的 api,我在 ManyToMany 中添加元素时遇到问题关系 例子, 模型.py class Picture(models.db): """ A pict
我正在尝试创建一个具有 0 到无限评论的资源(观察)。我陷入了以下错误: "error": "The model '' has an empty attribute 'comments' and do
我无法让这个为我的生活工作。 我在 api.py 中有这个 class catResource(ModelResource): class Meta: queryset = c
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 4年前关闭。 Improve this qu
我正在使用 Tastypie 为 Django 应用程序创建 REST API,并希望能够在一个 POST 中创建新对象和相关对象。相关对象由用于查找它们的名称指定,如果找不到名称,我想创建新对象。
我是一名优秀的程序员,十分优秀!