【问题标题】:Django Framework Rest match calendar users with current userDjango Framework Rest 将日历用户与当前用户匹配
【发布时间】:2022-06-12 19:57:53
【问题描述】:

我正在做一个练习,目标是让我当前的日历与其他用户相匹配。 为此,我创建了一个 UserProfile App 和 Schedule App。每个用户都有一个可以有多个间隔的配置文件。

考虑到我当前的日历:

{
"count": 1,
"next": null,
"previous": null,
"results": [
    {
        "id": 3,
        "user": {
            "id": 3,
            "username": "john.doe",
            "first_name": "John",
            "last_name": "Doe"
        },
        "calendar": [
            {
                "id": 1,
                "mon": true,
                "tue": true,
                "wed": true,
                "thu": true,
                "fri": true,
                "sat": true,
                "sun": true,
                "start_date": "09:30",
                "end_date": "12:20"
            },
            {
                "id": 2,
                "mon": true,
                "tue": true,
                "wed": true,
                "thu": true,
                "fri": true,
                "sat": true,
                "sun": true,
                "start_date": "14:00",
                "end_date": "23:00"
            }
        ]
    }
]}

当我调用端点 /api/search/users 时,它会返回所有用户配置文件以及来自每个用户的信息。

示例:

{
"count": 99,
"next": "http://localhost:8000/api/search/users?page=2",
"previous": null,
"results": [
    {
        "id": 1,
        "user": {
            "id": 1,
            "username": "john.bender.99",
            "first_name": "John",
            "last_name": "Bender"
        },
        "calendar": [
            {
                "id": 2,
                "mon": true,
                "tue": true,
                "wed": true,
                "thu": false,
                "fri": true,
                "sat": false,
                "sun": false,
                "start_date": "09:30",
                "end_date": "12:20"
            },
            {
                "id": 55,
                "mon": false,
                "tue": true,
                "wed": true,
                "thu": false,
                "fri": true,
                "sat": false,
                "sun": false,
                "start_date": "14:30",
                "end_date": "19:20"
            }
        ]
    }
]}

现在,我实际上想做的是用我的日历搜索相关用户,以了解我们匹配的日期/时间。

当我调用这个端点 /api/search/users?related=self 时,我想看到这个

{
"count": 2,
"results": [
    {
        "id": 87,
        "user": {
            "id": 87,
            "username": "diana.taller",
            "first_name": "Diana",
            "last_name": "Taller"
        },
        "calendar": [
            {
                "id": 2,
                "mon": true,
                "tue": true,
                "wed": true,
                "thu": false,
                "fri": true,
                "sat": false,
                "sun": false,
                "start_date": "10:30",
                "end_date": "11:20"
            },
            {
                "id": 55,
                "mon": false,
                "tue": true,
                "wed": true,
                "thu": false,
                "fri": true,
                "sat": false,
                "sun": false,
                "start_date": "16:30",
                "end_date": "17:20"
            }
        ]
    },{
        "id": 128,
        "user": {
            "id": 128,
            "username": "therockjosh",
            "first_name": "Josh",
            "last_name": "Bail"
        },
        "calendar": [
            {
                "id": 2,
                "mon": false,
                "tue": false,
                "wed": false,
                "thu": false,
                "fri": true,
                "sat": false,
                "sun": false,
                "start_date": "10:30",
                "end_date": "11:20"
            },
            {
                "id": 55,
                "mon": false,
                "tue": false,
                "wed": false,
                "thu": false,
                "fri": true,
                "sat": true,
                "sun": true,
                "start_date": "14:30",
                "end_date": "17:00"
            }
        ]
    }
]}

我的可用性和用户之间的拦截是在每天和每个间隔之间进行的,以查看我们何时匹配。

在我的搜索应用程序中,我创建了这个

        if related == "self":
        current_user_profile = UserProfile.objects.filter(user=self.request.user)
        related_users = UserProfile.objects.filter(calendar__in=current_user_profile.calendar.all())

        return related_users

如果我调用 current_user_profile,则返回我之前提供给您的当前用户数据。 如果我调用 UserProfile.objects.all(),则返回我之前提供给您的用户数据。

但由于某种原因,我无法从 current_user_profile.calendar 调用日历,如图所示。

有人知道我该怎么做吗? 谢谢。

【问题讨论】:

    标签: django django-rest-framework matching django-users


    【解决方案1】:

    如果你想获取对象,我认为你需要使用get 函数。

    if related == "self":
        # not UserProfile.objects.filter in order to get the UserProfile object.
        current_user_profile = UserProfile.objects.get(user=self.request.user)
        related_users = UserProfile.objects.filter(calendar__in=current_user_profile.calendar.all())
    
        return related_users
    

    【讨论】:

    • 这实际上离我想要的不是很远,但现在看起来我需要从结果中排除我的用户。这个建议给我带来了 diana.taller,但我的用户的次数也有几十次。也许缺少了什么。无论如何,感谢您的贡献
    【解决方案2】:

    这里我们找到了从搜索中排除我的用户的解决方案。

        current_user_profile = UserProfile.objects.get(user=self.request.user)
        related_users = UserProfile.objects\
            .filter(calendar__in=current_user_profile.calendar.all()) \
            .exclude(user_id=current_user_profile.id)
    
        return related_users
    

    【讨论】:

      猜你喜欢
      • 2013-03-24
      • 2014-09-17
      • 1970-01-01
      • 1970-01-01
      • 2016-06-01
      • 2022-01-17
      • 1970-01-01
      • 2011-04-04
      • 1970-01-01
      相关资源
      最近更新 更多