插入相关模型的主键 - django tastype

时间:2014-05-25 15:56:28

标签: python json django rest tastypie

我有2张桌子

  1. Mobilizer(它包含用户详细信息以及移动号码)

  2. smsdata(保持数据从特定的moblizer发送)

  3. 我需要使用sms服务器的json帖子..我正在使用django tastypie。我的json输入如下

    {"Df": 11.0, "Dm": 1.0, "Jf": 1.0, "Jm": 1.0, "Of": 2.0, "Om": 2.0, "Tf": 2.0, "mobile_no": '98xxxxxxxxx'}
    

    smsdata的表结构

    df dm jf jm of om tf mobilizer_id
    

    所以问题是如何解决mobile_no中的mobilizer_id并将其重新插入smsdata

    到目前为止,我在下面尝试了

    from tastypie.resources import ModelResource
    from indicators.models import Smsdata,Sfields
    from geography.models import mobilizer
    from tastypie import fields
    from tastypie.authorization import Authorization
    from tastypie.authentication import BasicAuthentication
    
    #to override authentication for POST,PUT and DELETE only
    class MyCustomAuthentication(BasicAuthentication):
        def is_authenticated(self, request, **kwargs):
            if request.method == 'GET':
                return True
            return super(MyCustomAuthentication,self).is_authenticated(request, **kwargs)
    
    class smsResource(ModelResource):
        #mobilizer = fields.ToManyField('path.to.api.mobilizerResource', attribute='mobilizer', related_name='sms')
        #field = fields.ToManyField('path.to.api.fieldsResource', attribute='field', related_name='sms1')
    
        class Meta:
            queryset = Smsdata.objects.all()
            resource_name = 'sms-sent'
            authorization = Authorization()
            authentication = MyCustomAuthentication()
    
    class mobilizerResource():
        sms = fields.ToOneField(smsResource, 'sms',related_name='mobilizer')
    
        class Meta:
            queryset = mobilizer.objects.all()
            resource_name = 'mobilizer'
    
    class fieldsResource():
        sms1 = fields.ToOneField(smsResource, 'sms',related_name='field')
    
        class Meta:
            queryset = Sfields.objects.all()
            resource_name = 'fields'
    

0 个答案:

没有答案