sumit
sumit

Reputation: 15464

returning json output from multiple table in django tastypie

I have extended django User with my custom fields , now i need to return a json output from custom table along with username form parent table .

I tried select_related in query set but it is not returning username

models

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)

resources

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

Anything missing from my codes?

Upvotes: 2

Views: 1293

Answers (1)

JamesO
JamesO

Reputation: 25946

You don't need select related here.

If you just want the username not the user object add a attribute field and you can do the normal Django __ relations e.g.

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

Upvotes: 8

Related Questions