如何从 TastyPie 中的两个 ForeignKey 字段获取数据?
How can I get data from two ForeignKey fields in TastyPie?
我有两个模型。
class Eatery(models.Model):
class Meta:
db_table = 'eatery'
date_pub = models.DateTimeField(auto_now_add=True)
name = models.CharField(max_length=54, blank=True)
description = models.TextField(max_length=1024)
approve_status = models.BooleanField(default=False)
author = models.ForeignKey(User, null=False, blank=True, default = None, related_name="Establishment author")
class Comments(models.Model):
class Meta:
db_table = 'comments'
eatery = models.ForeignKey(Eatery, null=False)
author = models.ForeignKey(User, null=False)
date_pub = models.DateTimeField(auto_now_add=True)
approve_status = models.BooleanField(default=True)
description = models.TextField(max_length=512)
我的 TastyPie 模型:
class EateryCommentsResource(ModelResource):
user = fields.ToOneField(UserResource, 'author', full=True)
class Meta:
queryset = Comments.objects.all()
resource_name = 'comments_eatery'
filtering = {
'author': ALL_WITH_RELATIONS
}
include_resource_uri = False
#always_return_data = True
paginator_class = Paginator
class EateryResource(ModelResource):
user = fields.ToOneField(UserResource, 'author', full=True)
comments = fields.ForeignKey(EateryCommentsResource, 'comments', full=True)
class Meta:
queryset = Eatery.objects.all()
#excludes = ['description']
resource_name = 'eatery'
filtering = {
'author': ALL_WITH_RELATIONS,
'comments': ALL_WITH_RELATIONS,
}
fields = ['user', 'comments']
allowed_methods = ['get']
serializer = Serializer(formats=['json'])
include_resource_uri = False
always_return_data = True
paginator_class = Paginator
authorization = DjangoAuthorization()
我无法获得带有评论的 EateryResource。当我没有评论时,它起作用了。如何使用 UserResource 和 CommentsResource 获取 EateryResourse。
对不起我的英语不好。谢谢。
由于评论通过外键链接到您的餐馆,因此您需要像这样定义 EateryResource
:
class EateryResource(ModelResource):
user = fields.ToOneField(UserResource, 'author', full=True)
comments = fields.ToManyField(EateryCommentsResource, 'comment_set', full=True)
我有两个模型。
class Eatery(models.Model):
class Meta:
db_table = 'eatery'
date_pub = models.DateTimeField(auto_now_add=True)
name = models.CharField(max_length=54, blank=True)
description = models.TextField(max_length=1024)
approve_status = models.BooleanField(default=False)
author = models.ForeignKey(User, null=False, blank=True, default = None, related_name="Establishment author")
class Comments(models.Model):
class Meta:
db_table = 'comments'
eatery = models.ForeignKey(Eatery, null=False)
author = models.ForeignKey(User, null=False)
date_pub = models.DateTimeField(auto_now_add=True)
approve_status = models.BooleanField(default=True)
description = models.TextField(max_length=512)
我的 TastyPie 模型:
class EateryCommentsResource(ModelResource):
user = fields.ToOneField(UserResource, 'author', full=True)
class Meta:
queryset = Comments.objects.all()
resource_name = 'comments_eatery'
filtering = {
'author': ALL_WITH_RELATIONS
}
include_resource_uri = False
#always_return_data = True
paginator_class = Paginator
class EateryResource(ModelResource):
user = fields.ToOneField(UserResource, 'author', full=True)
comments = fields.ForeignKey(EateryCommentsResource, 'comments', full=True)
class Meta:
queryset = Eatery.objects.all()
#excludes = ['description']
resource_name = 'eatery'
filtering = {
'author': ALL_WITH_RELATIONS,
'comments': ALL_WITH_RELATIONS,
}
fields = ['user', 'comments']
allowed_methods = ['get']
serializer = Serializer(formats=['json'])
include_resource_uri = False
always_return_data = True
paginator_class = Paginator
authorization = DjangoAuthorization()
我无法获得带有评论的 EateryResource。当我没有评论时,它起作用了。如何使用 UserResource 和 CommentsResource 获取 EateryResourse。 对不起我的英语不好。谢谢。
由于评论通过外键链接到您的餐馆,因此您需要像这样定义 EateryResource
:
class EateryResource(ModelResource):
user = fields.ToOneField(UserResource, 'author', full=True)
comments = fields.ToManyField(EateryCommentsResource, 'comment_set', full=True)