Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从django tastypie中的多个表返回json输出_Python_Django_Orm_Tastypie_Django Queryset - Fatal编程技术网

Python 从django tastypie中的多个表返回json输出

Python 从django tastypie中的多个表返回json输出,python,django,orm,tastypie,django-queryset,Python,Django,Orm,Tastypie,Django Queryset,我已经用我的自定义字段扩展了django User,现在我需要从自定义表返回json输出以及username表单父表 我尝试在查询集中选择\u related,但它没有返回username 模型 class ExProfile(models.Model): user = models.ForeignKey(User, unique=True) cell_phone = models.CharField(max_length=200, blank=True) api_key

我已经用我的自定义字段扩展了
django User
,现在我需要从自定义表返回json输出以及
username
表单父表

我尝试在查询集中选择\u related,但它没有返回
username

模型

class ExProfile(models.Model):
    user = models.ForeignKey(User, unique=True)
    cell_phone = models.CharField(max_length=200, blank=True)
    api_key=      models.CharField(max_length=200, blank=True)
    termination_date=models.DateField()
    picture=models.ImageField(upload_to='profile',blank=True)
    email=models.EmailField()
    homeAddress=models.CharField(max_length=200,blank=True)
    homeNumber=models.CharField(max_length=200,blank=True)
资源

class ProfileResource(ModelResource):

    class Meta:
         # the queryset below is working like ExProfile.objects.all() as it is not
         # returning username in json   
         queryset =ExProfile.objects.select_related('User').all()
         resource_name = 'entry'
         fields = ['username','api_key','email','homeAddress']   
         #authorization = Authorization()
         #authentication = MyAuthentication()
         filtering = {
             'api_key': ALL,
             'homeAddress': ALL,
             'email': ALL,
             'query': ['icontains',],
             }
         def apply_filters(self, request, applicable_filters):
                base_object_list = super(ProfileResource, self).apply_filters(request, applicable_filters)

                query  = request.META.get('HTTP_AUTHORIZATION')
                if query:
                    qset = (
                        Q(api_key=query)
                        )
                    base_object_list = base_object_list.filter(qset).distinct()

                return base_object_list

我的代码有什么遗漏吗?

您不需要在此处选择“相关”

如果您只需要用户名而不是用户对象,请添加一个属性字段,您可以执行正常的Django关系,例如

class ProfileResource(ModelResource):
    uname = fields.CharField(attribute='user__username', readonly=True)
    class Meta:
        queryset =ExProfile.objects.all()
        ....