gpt4 book ai didi

python - 如何过滤掉有约会(外键关系)的个人资料?

转载 作者:行者123 更新时间:2023-12-01 00:30:21 25 4
gpt4 key购买 nike

我正在尝试检索所有未安排AppointmentProfile 对象。 Appointment 对象可能有也可能没有与之关联的Profile(可以是空的时间段,也可以是已预订的约会)。

我还没有尝试过任何东西,因为我不知道从哪里开始。

用户/模型.py

class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
phone = models.CharField(max_length=30)
objects = managers.ProfileManager()

预订/models.py

class Appointment(models.Model):
profile = models.ForeignKey(
'users.Profile',
on_delete=models.PROTECT,
unique=False,
null=True,
)
massage = models.CharField(
default=None,
max_length=2,
choices=[
('SW', 'Swedish'),
('DT', 'Deep Tissue'),
],
null=True,
)
date_start = models.DateTimeField()
date_end = models.DateTimeField()
black_out = models.BooleanField(default=False)
date_created = models.DateTimeField(auto_now_add=True)
date_updated = models.DateTimeField(auto_now=True)
objects = AppointmentManager()

用户/managers.py

def search_by_name(self, request):
first_name = request.GET.get('first-name', '')
last_name = request.GET.get('last-name', '')

profiles = []
max_results = 5
if first_name == '' and last_name == '':
return (True, {'profiles': profiles})
elif last_name == '':
profiles = models.Profile.objects \
.filter(user__first_name__icontains=first_name)[:max_results]
elif first_name == '':
profiles = models.Profile.objects \
.filter(user__last_name__icontains=last_name)[:max_results]
else:
profiles = models.Profile.objects \
.filter(user__first_name__icontains=first_name) \
.filter(user__last_name__icontains=last_name)[:max_results]

return (True, {'profiles': profiles})

search_by_name 函数会过滤所有包含名字和/或姓氏的 Profile 对象,包括那些已安排Appointment 的对象(我不这样做)不想)。

感谢任何帮助。

最佳答案

您可以获得没有预约的个人资料:

Profile.objects.filter(<b>appointment=None</b>)

因此您可以使用以下内容进行过滤:

def search_by_name(self, request):
first_name = request.GET.get('first-name')
last_name = request.GET.get('last-name')
max_results = 5
filter = {}
if first_name:
filter['user__first_name__icontains'] = first_name
if last_name:
filter['user__last_name__icontains'] = first_name
profiles = models.Profile.objects.filter(
<b>appointment=None</b>,
**filter
)
return (True, {'profiles': profiles})

关于python - 如何过滤掉有约会(外键关系)的个人资料?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58236590/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com