简体   繁体   中英

Django - Foreign key, set of ids

Let's say that models are construct like this :

class Worker(models.Model):
    name_char = models.CharField(max_length=4,null=True, blank=True)
    body_parts_mtm = models.ManyToManyField('BodyPart')

class Job(models.Model):
    job_name = models.CharField(max_length=6,unique=True)  
    job_reference_mtm = models.ManyToManyField('JobReferenceCode') 

class JobReferenceCode(models.Model):
    job_ref_char = models.CharField(max_length=13)
    worker_mtm = models.ManyToManyField('Worker')

class BodyPart(models.Model):
    body_part_name_text = models.TextField()

class MembersSimilarity(models.Model):
    similarity_score_float = models.FloatField(max_length=10)
    worker_fk = models.ForeignKey(Worker,on_delete=models.CASCADE)
    job_fk = models.ForeignKey(Job,on_delete= models.CASCADE)

    #not sure if I need this field to do what I want but here it is:
    bodypart_fk = models.ForeignKey(BodyPart,on_delete=models.CASCADE)

On my website user can look for a JobReference, and I would like to give a specific output : a table where number of lines is controlled by combination of (Job,[BodyParts]).

In order to do it, on my view , I think what a found to solve this problem is to make a function that that has this structure (simplified):

job_ref_code = 1
job_query = Job.objects.filter(job_reference_mtm=job_ref_code)

for job in unique_job_query:
    sims = MembersSimilarity.objects.filter(job_fk=job)
    workers_from_sim= Worker.objects.filter(id__in=sims.values('worker_fk'))
    unique_ids_list = []
    for worker in workers_from_sim:
        combination = set(worker.cath_mtm.all())
        if combination not in unique_ids_list:
            unique_ids_list.append(combination)
        #All of this "for worker" loop to construct this list; do I need to acces like it ? Let say this list has this structure = [[1,2,3],[1],[1,2]]
    for body_part_combination in unique_body_ids_list:
         sim_queryset=MembersSimilarity.objects.filter(job_fk=job_query,bodypart_fk=body_part_combination)

#Note sim_query_set : if I can access to these similarities here (specific similarities of a job and a combination of body parts, my problem will be solved.

Is it possible to filter something like this ? I need to make a distinction with workers having specifics body parts and it for each job. I looked for how to do it and did not find anything, I am asking this question also to have your opinion about how to optimise my view function (eg ; loop to construct the distinct set of bodypart ids ...)

I know that this question is quite huge, but I am struggling since days now, and tried a lot of different model structure ... Any help would be more than appreciate, thank you !

I do not know if it I have to do it but with @Mani help, I have been able to find where to look for, and found this topic : Davor Lucic's answer and finally solved my problem.

In my case I tried all his answer and finally chose to make a .filter loop. According to my question, I solved this problem :

pre_queryset = #a query set of MembersSimilarity
post_queryset = pre_queryset.filter(job_fk=1,bodypart‌​_fk=1 AND 2)

Let say I have a list of body_part_fk, and want to filter over a pre_queryset :

list_ids=[1,2]
i=0
while i < len(list_ids)-1:
    if i==0: #use the prequeryset
        post_queryset = pre_queryset.filter(job_fk=job,bodypart_fk=list_ids[i])
    else:
        post_queryset = post_queryset.filter(job_fk=job,bodypart_fk=list_ids[i])
    i+=1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM