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

ジャンゴモデル

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