如何根据模型的另一个外键属性进行过滤

时间:2016-06-24 11:51:42

标签: python django django-models django-queryset

我正在开发一种算法来匹配约会请求和创建约会。问题的相关模型

Slot:
    day (enum, 0-6)
    start_time
    end_time

User:
    available_slots (day)

AppointmentRequest:
    user
    week_starting (monday's date)

Appointment:
    users
    week_starting
    slot (monday's date)

有很多标准,请求数量很少,因此每周循环使用一次约会请求似乎是一个合理的选择:

for appointment_request in appointment_requests:

    user_appointment_slots = Appointment.objects.filter(Q(week_starting=appointment_request.weel    ) &
                                                       (Q(first_user=appointment_request.user) |
                                                        Q(second_user=appointment_request.user))).slot_set.all()

    user_available_slots = appointment_request.user.available_slots_set.all() \
                .exclude(pk__in=user_appointment_slots)


    potential_requests = appointment_requests \
                .filter(user__available_slots__id__in=user_available_slots) \
                .exclude(user=appointment_request.user)

但是,在过滤appointment_requests以创建potential_requests时,我需要排除potential_requests'用户'已填充其他Appointment个对象的可用插槽。我相信我需要这样的东西:

.exclude(user__available_slots__pk__in=<what_to_put_here>)

基本上,<what_to_put_here>应该为我提供该对象(类型为AppointmentRequest),User&#39; s Appointment s&#39;插槽。我将不胜感激任何帮助。谢谢!

0 个答案:

没有答案