1

希望可以有人帮帮我!!:)

我关注了这个线程django-tastypie 和多对多“通过”关系 ,我确实通过表格成功地创建了多对多关系。

但我的问题是我无法将数据发布到桌上 manyTomany,让我自己解释一下。

表 A 表 B

关系表 AB

我想发送:{A:{AB:[ab,ab]}},我在 A 中发帖,但即使没有出错,数据也不会持久保存。

您可以在下面找到我的代码和我使用的 CURL。我想我必须使用 save_m2m 但我不知道怎么用!!!!!!有人可以指导如何做到这一点,在谷歌上找不到很好的信息

curl --dump-header - -H "Accept: application/json" -H "Content-Type: application/json"  -X put --data '{"agents":[],"director":"/api/v1/user/5/","disco":"/api/v1/disco/1/","doors":[],"end_date":"2013-05-02T14:41:00","ini_date":"2013-05-02T14:41:00","name":"hrthhrthrth","offers":[{"id":108,"offer":"/api/v1/offer/5/","party":"/api/v1/party/61/","resource_uri":"/api/v1/partyoffer/108/","state":"FALSE","updated_at":"2013-05-19T17:05:03.262410"},{"id":107,"offer":"/api/v1/offer/4/","party":"/api/v1/party/61/","resource_uri":"/api/v1/partyoffer/107/","state":"TRUE","updated_at":"2013-05-19T17:08:08.336766"}],"resource_uri":"/api/v1/party/61/","updated_at":"2013-05-20T19:31:42.256Z","sid":61,"dirty":true}' 'http://127.0.0.1:8001/api/v1/party/61/?username=director1&api_key=ee1ea29c028879f76b690e224d884808232b8e3e'

好吃的api

class PartyResource(ModelResource):
    disco = fields.ToOneField('disco_restapi.api.DiscoResource','disco',full=False,null=True)
    director = fields.ToOneField('disco_restapi.api.UserResource','director',null=True)
    doors = fields.ToManyField('disco_restapi.api.UserResource','party_doors',full=False,null=True)

    agents = fields.ToManyField('disco_restapi.api.PartyUserResource',
        attribute=lambda bundle:
            bundle.obj.party_agents.through.objects.filter(party=bundle.obj)
            or bundle.obj.party_agents, full=True, null=True)

    offers = fields.ToManyField('disco_restapi.api.PartyOfferResource',null=True,
        attribute=lambda bundle:
            bundle.obj.party_offers.through.objects.filter(party=bundle.obj)
            or bundle.obj.party_offers, full=True)

    class Meta:
        queryset = Party.objects.all()
        detail_allowed_methods = ["get","patch","put","post"]
        authentication = ApiKeyAuthentication()
        authorization = Authorization()
        always_return_data = True
        filtering = {
            'party':ALL_WITH_RELATIONS,
            'disco':ALL_WITH_RELATIONS,
            'agents': ALL_WITH_RELATIONS,
            'director': ALL_WITH_RELATIONS,
            'doors': ALL_WITH_RELATIONS,
            'ini_date':ALL,
            'end_date':ALL,
            'name':ALL,
            'updated_at' : ['gte']
        }
        resource_name = 'party'

    def determine_format(self, request):
        return 'application/json'

    def dehydrate(self, bundle):
        return bundle

    def save_m2m(self, bundle):
        return  bundle

class PartyUserResource(ModelResource):
    user = fields.ToOneField(UserResource, 'user',full=False)
    party = fields.ToOneField(PartyResource, 'party',full=False)
    class Meta:
        queryset= PartyUser.objects.all()
        always_return_data = True
        detail_allowed_methods = ["get","patch","put","post"]
        authentication = ApiKeyAuthentication()
        authorization = Authorization()

    def determine_format(self, request):
            return 'application/json'

class PartyOfferResource(ModelResource):
    offer = fields.ToOneField(OfferResource, 'offer', full=False)
    party = fields.ToOneField(PartyResource, 'party', full=False)

    class Meta:
        queryset= PartyOffer.objects.all()
        always_return_data = True
        detail_allowed_methods = ["get","patch","put","post"]
        authentication = ApiKeyAuthentication()
        authorization = Authorization()

    def dehydrate_name(self,bundle):
        return bundle.obj.party.name

    def determine_format(self, request):
        return 'application/json'

    def save_m2m(self, bundle):
            return  bundle

django模型

class Party(models.Model):
    disco =  models.ForeignKey(Disco,related_name='parties')
    director = models.ForeignKey(User,related_name='parties_director')

    party_offers = models.ManyToManyField(Offer, through='PartyOffer', null=True,blank=True)
    party_agents = models.ManyToManyField(User,  through='PartyUser',  null=True,blank=True)

    party_doors = models.ManyToManyField(User, related_name='parties_door',null=True,blank=True)

    name = models.CharField(max_length=150)
    ini_date = models.DateTimeField()
    end_date = models.DateTimeField()
    updated_at = models.DateTimeField(auto_now=datetime.now(), default=datetime.now())

class PartyUser(models.Model):
    party = models.ForeignKey(Party)
    user = models.ForeignKey(User)
    state = models.CharField(max_length=5, null=True, blank =True)
    updated_at = models.DateTimeField(auto_now=datetime.now(), default=datetime.now())

class PartyOffer(models.Model):
    party = models.ForeignKey(Party)
    offer = models.ForeignKey(Offer)
    state = models.CharField(max_length=5, null=True, blank =True)
    updated_at = models.DateTimeField(auto_now=datetime.now(), default=datetime.now())

谢谢!!!!!

4

0 回答 0