获取登录用户和另一个模型的值 - Tastypie

Get logged in user and value of another model - Tastypie

型号:

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

资源:

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'

今天是我尝试 Tastypie 的第一天,所以请善待我:(

JobListingResource return 是所有职位列表。但我只想获取 Tags 列包含已登录用户 skill 列值的职位列表。

例如:如果用户"A"已经登录并拥有以下技能"python,django,jquery"。我只希望 JobListingResource 到 return 那些在标签列中包含 [python/django/jquery] 的记录。

我假设您知道如何进行查询,只需要知道在 Tastypie 中的何处进行查询。在您的 JobListResource 中覆盖如下:

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)