2

I am doing an exercise where the goal it's to match my current calendar with other users. To do this, I created a UserProfile App and Schedule App. Each user has a profile that can have multiple intervals.

Considering my current calendar:

{
"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"
            }
        ]
    }
]}

When I am doing a call to the endpoint /api/search/users it returns all User Profiles with info from each user.

example:

{
"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"
            }
        ]
    }
]}

Now, what I want to do actually is a search for related users with my calendar to know what days/hours we have a match.

When I do a call to this endpoint /api/search/users?related=self, I want to see this

{
"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"
            }
        ]
    }
]}

The interception between my availability and from users is done between per day and then each interval to see when we have a match.

Inside my Search App, I created this

        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

queryset attribute calendar

If I call current_user_profile, returns me the current user data as I provided you before. If I call UserProfile.objects.all(), returns me the user's data as I provided you before.

But for some reason, I can't call calendar from current_user_profile.calendar as this image shows.

Is anyone have some idea how could I do this?

Sunderam Dubey
  • 1
  • 11
  • 20
  • 40

2 Answers2

0

I think you need to use get function if you wanna get the object.

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
Metalgear
  • 3,391
  • 1
  • 7
  • 16
  • This is actually not very far from what I want but looks like now I need to exclude my user from the result. This suggestion brings me diana.taller but also the amount of times from my user is dozens. Maybe something is missing. anyway, thanks for the contribution –  Jun 12 '22 at 10:35
0

Here we have the solution I found to exclude my user from the search.

    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