0

执行 Get (TastyPie) 我收到以下错误:

error_message": "'CompanyUser' 对象没有属性 'all'",/resources.py\",第 832 行,在 full_dehydrate\n bundle.data[field_name] = field_object.dehydrate(bundle, for_list=for_list)\n\ n 文件 \"/Users/user/Documents/workspace/membergetmember/django-env/lib/python2.7/site-packages/tastypie/fields.py\",第 818 行,在 dehydra\n 中为 the_m2ms.all 中的 m2m ():\n\nAttributeError: 'CompanyUser' 对象没有属性 'all'\n"

根据我的理解,这应该有效,我做错了什么?代码如下。

class UserResource(ModelResource):
    class Meta:
        queryset = get_user_model().objects.all()
        resource_name = 'CompanyUser'

class CampaignCreateResource(ModelResource):
    user = fields.ToManyField(UserResource, 'user', full=False)

    class Meta:
        queryset = Campaign.objects.all()
        resource_name = 'campaign'
        allowed_methods = ['post', 'get']
        authentication = APIAuthentication().get_authentication()
        authorization = UserObjectsOnlyAuthorization()
        validation = FormValidation(form_class=CampaignForm)
        excludes = ['id']

模型

class Campaign(models.Model):
    name = models.CharField(max_length=60, help_text="Give your campaign a name i.e Xmas Offer")
    user = models.ForeignKey(get_user_model(),  related_name='user', null=True)
4

1 回答 1

1

尝试:

user = fields.ToOneField(UserResource, 'user',  full=True)

注意: ToOneField然后我们添加full=true以显示所有详细信息。

于 2013-10-28T13:05:20.033 回答