django - 用Tastypie创建相关资源

标签 django tastypie

我想由于我发布到UserResource而创建了一个UserProfileResource。

models.py:

class UserProfile(models.Model):
    home_address = models.TextField()
    user = models.ForeignKey(User, unique=True)

resources.py
class UserProfileResource(ModelResource):
    home_address = fields.CharField(attribute='home_address')

    class Meta:
        queryset = UserProfile.objects.all()
        resource_name = 'profile'
        excludes = ['id']
        include_resource_uri = False


class UserResource(ModelResource):
    profile = fields.ToOneField(UserProfileResource, 'profile', full=True)
    class Meta:
        queryset = User.objects.all()
        resource_name = 'user'
        allowed_methods = ['get', 'post', 'delete', 'put']
        fields = ['username']
        filtering = {
                'username': ALL,
                }

curl命令:
curl -v -H "Content-Type: application/json" -X POST --data '{"username":"me", "password":"blahblah", "profile":{"home_address":"somewhere"}}' http://127.0.0.1:8000/api/user/

但我得到:
 Django Version:   1.4
 Exception Type:   IntegrityError
 Exception Value:
 null value in column "user_id" violates not-null constraint

好像是鸡和鸡蛋的场景。我需要user_id来创建UserProfileResource,并且我需要配置文件来创建UserResource。显然我做的事很愚蠢。

外面有人可以发光吗?
非常感谢
约诺克

我按照Pablo的建议修改了我的代码。
class UserProfileResource(StssRessource):
    home_address = fields.CharField(attribute='home_address')
    user = fields.ToOneField('resources.UserResource', attribute='user', related_name='profile')

    class Meta:
        queryset = UserProfile.objects.all()
        resource_name = 'profile'


class UserResource(ModelResource):
    profile = fields.ToOneField('resources.UserProfileResource', attribute='profile', related_name = 'user', full=True)
    class Meta:
        queryset = User.objects.all()
        resource_name = 'user'

但是越来越:
 Django Version:   1.4
 Exception Type:   DoesNotExist

这与尝试访问ORM中的用户资源有关,该资源在创建related_objects UserProfileResource时不存在。哪个是对的。在创建related_object之后,才创建用户ORM。

其他人看到了吗?

最佳答案

两天后,我终于设法节省了相关资源,问题是您必须指定关系的两面及其相关名称,在您的情况下,将是这样的:

class UserProfileResource(ModelResource):
    home_address = fields.CharField(attribute='home_address')
    user = fields.ToOneField('path.to.api.UserResource', attribute='user', related_name='profile')
         #in my case it was a toManyField, I don't know if toOneField works here, you can try toManyField.

class UserResource(ModelResource):
    profile = fields.ToOneField(UserProfileResource, 'profile', related_name='user', full=True)

关于django - 用Tastypie创建相关资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10582449/

相关文章:

django - rabbitmq+celery 内存泄漏?

django - 在 ModelViewSet 中返回 422 状态码

python - Django 引发类型错误 "profile() got an unexpected keyword argument ' 用户'”

python - 在 Django 中,request.META[] 是为请求添加特定信息的正确位置吗?

django - 在Django-Tastypie中使用POST的ForeignKey的问题

python - 使用 django-crispy 组合布局时出错

django - 相同的字符串具有不同的翻译

django - 如何以编程方式使用django-tastypie API创建或注册用户?

django - Django + Tastypie 分析

javascript - 使用 $http 发送 JSON 导致 Angular 发送文本/纯内容类型