做一个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 File \“/ Users / user / Documents / workspace / membergetmember / django-env / lib / python2.7 / site-packages / tastypie / fields.py \” , 第818行,脱水\ n表示m2中的m2m.all():\ 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)
答案 0 :(得分:1)
尝试:
user = fields.ToOneField(UserResource, 'user', full=True)
注意: ToOneField
然后我们添加full=true
以显示所有详细信息。