Django-tastypie:不保存ToOneField

时间:2016-09-03 11:04:58

标签: django foreign-key-relationship tastypie bad-request

不能承受问题:

class PostResource(ModelResource):

    place   = fields.ToOneField(PlaceResource,  attribute='place')
    user    = fields.ToOneField(ProfileResource,attribute='profile')
    #action  = fields.ForeignKey(ActionResource, 'action')

    def hydrate(self, bundle):
    -------------------------
        bundle.data['place'] = _place
        bundle.data['user']  = qProfile.objects.get(user = bundle.request.user)
        return bundle


class ProfileResource(ModelResource):
    username    = fields.ForeignKey(UserResource, 'user', blank=True, null=True)
    class Meta:
        queryset = qProfile.objects.all()
        excludes = ['token']
        resource_name = 'profile'

    def dehydrate_username(self, bundle):
        bundle.data['username'] = bundle.obj.user.username
        return bundle.obj.user.username

class UserResource(ModelResource):
    class Meta:
        queryset = User.objects.all()
        excludes = ['email', 'password', 'is_active', 'is_staff', 'is_superuser']
        resource_name = 'user'

class PostInfo(models.Model):
    user        = models.ForeignKey(qProfile, related_name="PostUser",    on_delete=models.CASCADE, null=True, blank=True)
    text        = models.CharField(max_length=250)
    photos      = models.ForeignKey(PostPhoto, related_name="PostPhoto",    on_delete=models.CASCADE, null=True, blank=True)
    place       = models.ForeignKey(PlaceInfo, related_name="PostPlace",    on_delete=models.CASCADE, null=True, blank=True)
    action      = models.ForeignKey(ActionInfo, related_name="PostAction",    on_delete=models.CASCADE, null=True, blank=True)
    date        = models.DateTimeField(auto_now_add=True)

bundle.data ['place']正常保存,但无法保存用户。即使我将设置为bundle.data ['user'] bundle.request.user并将用户ForeinKey更改为UserResource问题仍然在这里。当我使用'action'定义取消注释这一行或行时,此水合物会返回Bad Request:

user    = fields.ToOneField(ProfileResource,attribute='profile')

0 个答案:

没有答案