简体   繁体   English

Django-外键,一组ID

[英]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]). 在我的网站上,用户可以查找JobReference,我想给出一个特定的输出:一个表,其中的行数由(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 ...) 我一直在寻找方法,却没有找到任何东西,我也在问这个问题,以征询您对如何优化我的视图函数的意见(例如;循环以构造不同的bodypart id集...)

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. 我不知道是否必须这样做,但是在@Mani的帮助下,我能够找到要查找的地方,并找到了这个主题: Davor Lucic的答案 ,终于解决了我的问题。

In my case I tried all his answer and finally chose to make a .filter loop. 以我为例,我尝试了所有他的答案,最后选择制作一个.filter循环。 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 : 假设我有一个body_part_fk列表,并且想要过滤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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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