Tastypie ManyToMany 中的 POST / PUT 错误

Posted

技术标签:

【中文标题】Tastypie ManyToMany 中的 POST / PUT 错误【英文标题】:Errors with POST / PUT in Tastypie ManyToMany 【发布时间】:2016-04-01 01:32:27 【问题描述】:

我在使用带有 Tastypie 的 ManyToMany 发布和放置数据时收到错误消息。 我花了几个小时试图自己弄清楚并查看了以下帖子:

Nesting tastypie UserProfileResource and UserResource, null value in column \"user_id\" violates not-null constraint Django Tastypie Many to Many (self) field update using a PATCH or PUT request?Django TastyPie Patch to a Many-to-Many Django Tastypie not Updating Resource with ManyToManyField django-tastypie: Posting to a Resource having ManytoMany field with through relationship

iapp/models.py

class Rankings(models.Model):

    rank_type = models.CharField(max_length=120,default='',blank=True,null=True)    
    rank = models.IntegerField()
    ranking_domain = models.CharField(max_length=120,default='',blank=True,null=True)
    ranking_url = models.CharField(max_length=120,default='',blank=True,null=True)

class Keyword(models.Model):

    keyword = models.CharField(max_length=120,default='',blank=True,null=True)
    u_ts = models.DateTimeField(auto_now_add=True,auto_now=False)
    state = models.CharField(max_length=120,default='',blank=True,null=True)
    s_ts = models.DateTimeField(blank=True,null=True)
    ip = models.CharField(max_length=120,default='',blank=True,null=True)
    rankings = models.ManyToManyField(Rankings,blank=True)

iapp/api/resources.py

class KeywordResource(ModelResource):   
    rankings = fields.ToManyField('iapp.api.resources.RankingsResource', 'rankings',null=True, full=True)
    class Meta:
        queryset = Keyword.objects.all()
        resource_name = 'keyword'
        allowed_methods = ['get','post','put','patch']
        authentication = ApiKeyAuthentication()
        authorization = DjangoAuthorization()
        filtering = "keyword": ('exact', 'startswith',),
                     "state": ('exact',)

class RankingsResource(ModelResource):

    class Meta:
        queryset = Rankings.objects.all()
        resource_name = 'rankings'
        fields = ['rank_type']
        allowed_methods = ['get','post','put','patch']
        authentication = ApiKeyAuthentication()
        authorization = DjangoAuthorization()

发布功能:

def add_keyword(self,**kwargs): 

    keyword = kwargs['keyword']     
    data = "keyword": keyword,
            "u_ts":None,
            "state":"added",
            "s_ts":None,
            "ip":None,
            "rankings":["rank_type":'typetest',
                                        "rank":0,
                                        "ranking_domain":'domaintest',
                                        "ranking_url":'urltest']
             

    req_post = requests.post(self.endpoints['keywords'],#dict for different endpoints
                             headers=self.headers,#my authentication
                             data=json.dumps(data)) 

错误:

NOT NULL constraint failed: iapp_rankings.rank

但是,数据(减去排名数据)被添加到数据库中。

任何帮助将不胜感激。

【问题讨论】:

【参考方案1】:

问题是,你有fields = ['rank_type']。 试试fields = ['rank_type', 'rank'],它应该可以工作。

【讨论】:

以上是关于Tastypie ManyToMany 中的 POST / PUT 错误的主要内容,如果未能解决你的问题,请参考以下文章

@ManyToMany @JoinTable 之类的注释,有啥作用?可以不写吗?

在一个 TastyPie API 请求中发布多个对象

Tastypie 不发送 cookie

使用 TastyPie 过滤不同的字段

如何通知应用程序凭据在 Django/Tastypie/REST 中成功

Django Tastypie,运行一个动作