Search code examples
pythondjangotastypie

Get logged in user and value of another model - Tastypie


Models:

class Applicant_Skill(models.Model):
    user = models.ForeignKey(User)

    #applicant = models.ForeignKey(Applicant)
    skill = models.ForeignKey('skills_layer.Skill')
    active = models.BooleanField(default=True)

class Job_Posting(models.Model):
    company = models.ForeignKey('companies_layer.Company', default=-1)
    job_posted_by = models.ForeignKey(User, default=-1)
    job_title = models.CharField(max_length=100)
    job_summary = HTMLField(blank=True)
    job_details = HTMLField(blank=True)
    no_of_openings = models.IntegerField(default=0)
    tags = models.CharField(max_length=200)
    experience_min = models.IntegerField(default=0)
    experience_max = models.IntegerField(default=0)
    job_location = models.ForeignKey('meta_data_layer.Location',  blank=True, null=True)
    qualification = models.ForeignKey('meta_data_layer.Qualification', default=-1)
    specialization = models.ForeignKey('meta_data_layer.Specialization', default=-1)
    nationality = models.ForeignKey('meta_data_layer.Nationality', default=-1)
    live = models.BooleanField(default=True)
    closing_date = models.DateField(default=datetime.date.today())
    auto_renew = models.BooleanField(default=False)
    active = models.BooleanField(default=True)

    class Meta:
        verbose_name = "job posting"

    def __str__(self):
        return self.job_title

Resource:

from tastypie.resources import ModelResource
from job_posting_layer.models import Job_Posting
from companies_layer.models import Company
from django.contrib.auth.models import User
import meta_data_layer
from tastypie import fields



class UserResource(ModelResource):
  class Meta:
    queryset = User.objects.all()
    resource_name = 'user'


    def dehydrate(self, bundle):
        bundle.data['logged_user_id'] = bundle.request.user.id
        return bundle




class JobListingResource(ModelResource):
    #company = fields.ForeignKey(CompanyResource,'company', full=True)
    #job_posted_by = fields.ForeignKey(UserResource,'job_posted_by', full=True)

    company_name = fields.CharField(attribute="company__company_name", null=True)
    company_id = fields.CharField(attribute="company__id", null=True)
    user_first_name = fields.CharField(attribute="job_posted_by__first_name", null=True)
    user_last_name = fields.CharField(attribute="job_posted_by__last_name", null=True)
    user_id = fields.CharField(attribute="job_posted_by__id", null=True)
    job_location = fields.CharField(attribute="job_location__location_name", null=True)
    job_city = fields.CharField(attribute="job_location__city", null=True)
    qualification = fields.CharField(attribute="qualification__qualification_degree", null=True)
    specialization = fields.CharField(attribute="specialization__specialization_course", null=True)
    nationality = fields.CharField(attribute="nationality__country_name", null=True)

    class Meta:
        queryset = Job_Posting.objects.all()
        resource_name = 'jobs'

Today is the 1st day I am trying Tastypie so please be kind with me :(

The JobListingResource returns all the Job Listings. But I want to get only those Job Listings for which the Tags column contains values from the skill column of the logged in user.

Eg: If user "A" has logged in and has the following skills "python,django,jquery". I want the JobListingResource to return only those records which contains [python/django/jquery] in the tags column.


Solution

  • I'm assuming you know how to do the queries and just need to know where to do it in Tastypie. In your JobListResource override as follows:

    def get_object_list(self, request):
    
        # get all the jobs according to the queryset in Meta
        base = super(JobListingResource, self).get_object_list(request)
    
        # and add a filter so only users ones appear
        user = request.user
        skills = query to get all the skills for the user
        return base.filter(filter to apply to JobPosting to only return jobs matching skills list)