当使用带有to_field的ForeignKey时,Django Tastypie - Put或Post = null

时间:2016-04-13 00:23:09

标签: django null foreign-keys tastypie

请注意,在Customer模型中,我有两个字段,其中包含City模型的ForeignKey + to_field。

当我执行PUT或POST时,这些字段始终为NULL。

有什么问题?

class City(models.Model):
    code_x = models.CharField(max_length=7, blank=True, null=True, unique=True, verbose_name=u'Code')
    name = models.CharField(max_length=50, blank=True, null=True, verbose_name=u'Name')

class Customer(models.Model):
    name = models.CharField(max_length=100, blank=True, null=True, verbose_name=u'Name')
    city_day = models.ForeignKey(City, to_field='code_x', blank=True, null=True, related_name='city_day', verbose_name= 'City at Day')
    city_night = models.ForeignKey(City, to_field='code_x', blank=True, null=True, related_name='city_night', verbose_name= 'City at Night')


 class CityResource(ModelResource):
    class Meta:
        queryset = City.objects.all()
        resource_name = 'cities'
        format = 'json'
        allowed_methods = ['get','post','put']
        authorization = DjangoAuthorization()
        authentication = BasicAuthentication()
        include_resource_uri = False
        always_return_data = True

class CustomerResource(ModelResource):    
    city_day = fields.ForeignKey(CityResource, 'city_day', null=True)
    city_night = fields.ForeignKey(CityResource, 'city_night', null=True)

    class Meta:
        queryset = Customer.objects.all()
        resource_name = 'customers'
        format = 'json'
        allowed_methods = ['get','post','put']
        authorization = DjangoAuthorization()
        authentication = BasicAuthentication()
        include_resource_uri = False
        always_return_data = True

0 个答案:

没有答案