2015-02-23 62 views
0

型號:獲取登錄的用戶和另一個模型的價值 - 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返回所有的工作列表。但我只想得到那些工作列表其中Tags列包含登錄用戶的skill列中的值。

例如:如果用戶「A」已經登錄並具有以下技能「python,django,jquery」。我希望JobListingResource僅返回tags列中包含[python/django/jquery]的記錄。

回答

1

我假設你知道如何做這些查詢,只需要知道在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) 
+0

老兄你真棒:)真的很感謝。 – Monodeep 2015-02-24 16:37:36