# Profile matching query does not exist.

**URL:** <https://forum.djangoproject.com/t/profile-matching-query-does-not-exist/20079>\
**Category:** Using the ORM\
**Created:** [April 5, 2023, 1:17pm UTC](https://forum.djangoproject.com/t/profile-matching-query-does-not-exist/20079 "2023-04-05T13:17:20Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Theresa-o](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/theresa-o/32/7565_2.png) [@Theresa-o](https://forum.djangoproject.com/u/Theresa-o)\
**Post date:** [April 5, 2023, 1:17pm UTC](https://forum.djangoproject.com/t/profile-matching-query-does-not-exist/20079/1 "2023-04-05T13:17:20Z")

</div>

I am trying to access a user’s profile, the user is logged in via admin but I get this error when trying to access the portal " [DoesNotExist at /profile/2](http://127.0.0.1:8000/profile/2)" - “Profile matching query does not exist.”.

How can I correct this please?

MODELS.PY

```auto
class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    bio = models.TextField(blank=True)
    follows = models.ManyToManyField("self", related_name="followed_by", symmetrical=False, blank=True)

    def __str__ (self):
        return self.user

```

URLS.PY

```auto
    path("profile/<int:user_id>", views.profile, name="profile"),

```

PROFILE.HTML

```auto
{% extends "network/layout.html" %}

{% block body %}
<div class="col-5 profile__wrapper">
    <div class="header__wrapper">
      <!-- <div class="header_headerimg">
        <img src="http://placeimg.com/640/480/nature" alt="User background image" />
      </div> -->
      <div class="cols__container cols-container">
        <div class="content__col">
          <div class="img-container">
            <img src="http://placeimg.com/140/140/people" alt="User image" />
            <span></span>
          </div>
        <form method="POST">
            {% csrf_token %}
            <h2>{{ profile_user }}</h2>
            <p>@{{ profile_user }}</p>
            <input type="hidden" value="{{user.username}}" name="follower">
            <input type="hidden" value="{{user_object.username}}" name="user">

            <strong>Follows</strong> <br/>
            
            {% for following in profile.follows.all %}
                @ {{ following }} <br/>
            {% endfor %}

            <strong>Followed by</strong> <br/>
            {% for following in profile.followed_by.all %}
                @ {{ following }} <br/>
            {% endfor %}

        </form>
</div>
      </div>
    </div>
</div>
{% endblock %}

```

VIEWS.PY

```auto
def profile(request, user_id):
    if request.user.is_authenticated:
        profile = Profile.objects.get(pk = user_id)
        context = {
            "profile": profile,
        }
        return render(request, "network/profile.html", context)
    else:
        context = {
        "profile": profile,
    }
        return render(request, "network/profile.html", context)

```

The error I see is coming from Views - profile = Profile.objects.get(pk = user\_id)

When I edit this to the below code, it shows but my profile name on Profile.html is blank i.e ’ {{ profile\_user }}’

```auto
        profile = User.objects.get(pk = user_id)

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 5, 2023, 1:35pm UTC](https://forum.djangoproject.com/t/profile-matching-query-does-not-exist/20079/2 "2023-04-05T13:35:43Z")

</div>

The issue here is that the `user` field in Profile is not necessarily the same value as the `id` field in that instance.

This means that:

> [@Theresa-o](#):
>
> Profile.objects.get(pk = user\_id)

and

> [@Theresa-o](#):
>
> `User.objects.get(pk = user_id)`

might be referring to two different objects.

If `user_id` is the pk of the `User` object, then you need to compare the foreign key in `Profile` to `User`.

In this case then it may look like:  
`Profile.objects.get(user_id=user_id)`

---

<div class="post-metadata">

**Author:** ![Theresa-o](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/theresa-o/32/7565_2.png) [@Theresa-o](https://forum.djangoproject.com/u/Theresa-o)\
**Post date:** [April 5, 2023, 1:42pm UTC](https://forum.djangoproject.com/t/profile-matching-query-does-not-exist/20079/3 "2023-04-05T13:42:58Z")

</div>

> [@KenWhitesell](#):
>
> Profile.objects.get(user\_id=user\_id)

This returns the same error

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 5, 2023, 1:48pm UTC](https://forum.djangoproject.com/t/profile-matching-query-does-not-exist/20079/4 "2023-04-05T13:48:17Z")

</div>

The error is specifically saying that you don’t have an instance of Profile that refers to that user.

What data do you have in the Profile model? (How many entries are in that table?) What user\_ids are present?)

---

<div class="post-metadata">

**Author:** ![Theresa-o](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/theresa-o/32/7565_2.png) [@Theresa-o](https://forum.djangoproject.com/u/Theresa-o)\
**Post date:** [April 5, 2023, 2:06pm UTC](https://forum.djangoproject.com/t/profile-matching-query-does-not-exist/20079/5 "2023-04-05T14:06:56Z")

</div>

> [@Theresa-o](#):
>
> `profile = Profile.objects.get(pk = user_id)`

I checked, I had no profile data showing in my admin panel until I added one, is there a way for me to inherit data directly from the User Model when a user is created i.e I would like if when a user is created, it automatically creates a profile for that same user and stores the data in the Profile Model.

> [@Theresa-o](#):
>
> User.objects.get(pk = user\_id)

This may be my best option if the above is not available as I would not like for a user to need to create profiles manually after their user account is created for this particular project.

What do you think?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 5, 2023, 2:08pm UTC](https://forum.djangoproject.com/t/profile-matching-query-does-not-exist/20079/6 "2023-04-05T14:08:50Z")

</div>

> [@Theresa-o](#):
>
> I would like if when a user is created, it automatically creates a profile for that same user and stores the data in the Profile Model.

Yes, that’s an appropriate approach.

How are you creating users? Through the admin, or do you have other views to do this?

---

<div class="post-metadata">

**Author:** ![Theresa-o](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/theresa-o/32/7565_2.png) [@Theresa-o](https://forum.djangoproject.com/u/Theresa-o)\
**Post date:** [April 5, 2023, 2:09pm UTC](https://forum.djangoproject.com/t/profile-matching-query-does-not-exist/20079/7 "2023-04-05T14:09:51Z")

</div>

Through admin, I don’t have a separate view for this

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 5, 2023, 2:11pm UTC](https://forum.djangoproject.com/t/profile-matching-query-does-not-exist/20079/8 "2023-04-05T14:11:55Z")

</div>

In that case, you might want to use the [`get_or_create`](https://docs.djangoproject.com/en/4.2/ref/models/querysets/#get-or-create) function for `Profile` instead of `get`. That will create the object the first time you try to reference it. It’s going to be easier to do it that way instead of adding something to the admin class.

---

<div class="post-metadata">

**Author:** ![guzmanojero](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/guzmanojero/32/1675_2.png) [@guzmanojero](https://forum.djangoproject.com/u/guzmanojero)\
**Post date:** [January 30, 2024, 10:10pm UTC](https://forum.djangoproject.com/t/profile-matching-query-does-not-exist/20079/9 "2024-01-30T22:10:22Z")

</div>

Let’s see what’s going on.

  

**1. Why are you having this error?**

Here is the thing:

Sometimes it could happen that the user id and the profile id differ.

So when you do:

```auto
profile = Profile.objects.get(pk = user_id)

```

You’re assuming that the profile id is the same as the user’s id. Which it could not be the case. Suppose you first created two users and later on you added the profile to your app. You already have two users (id 1 and 2). When you create a new user with id 3, this will have a profile with id 1, because it’s the first profile record.

An example will help:

A) The `auth_user` table:

| id | username |
| --- | --- |
| 1 | user\_1 |
| 2 | user\_2 |
| 3 | user\_3 |
| 4 | user\_4 |

B) The `<app_name>_profile` table, where `user_id` shows the relationship with `User`.

| id | user\_id |
| --- | --- |
| 1 | user\_3 |
| 2 | user\_4 |

Profile with id 1 has a relationship with `user_3`.  
Profile with id 2 has a relationship with `user_4`.

But `user_1` and `user_2` do not have a profile.

  

This is the model:

```auto
#models.py
class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)

```

If you want to get the user’s profile in a view, you should do something like this:

```auto
#views.py
def update_user(request, user_id_request):
    if request.user.is_authenticated:
        current_user = User.objects.get(id=user_id_request)
        current_profile = Profile.objects.get(user_id=user_id_request)

```

This is the critical part:

```auto
current_profile = Profile.objects.get(user_id=user_id_request)

```

I changed the parameter’s name in the view just to show a different name so you can see that user\_id refers to a column in the table `<app_name>_profile` and `user_id_request` is part of the request.

Your error `DoesNotExist at /profile/2` shows that there is not a profile for the referenced `user_id_request = 2`.

To me, when this kind of problems arise, I like to see the database directly and not the admin alone (Like the tables I showed above).

  

**1. How to create a profile automatically after creating a user?**

  

There are different ways.  
Here I comment one.

You can use Django’s signals to automatically create a `Profile` instance when a new `User` instance is created. Specifically, you can use the `post_save` signal. Here’s an example of how you can achieve this:

  

```auto
#models.py

from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)

    def __str__ (self):
        return self.user.username

def create_profile(sender, instance, created, **kwargs):
    if created:
        user_profile = Profile(user=instance)
        user_profile.save()

post_save.connect(create_profile, sender=User)

```

  

I hope this helped!! 💪

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 31, 2024, 1:42pm UTC](https://forum.djangoproject.com/t/profile-matching-query-does-not-exist/20079/10 "2024-01-31T13:42:36Z")

</div>

@guzmanojero - I appreciate you taking the time and effort to craft this post. However, this thread is more than 9 months old, and the person who posted it hasn’t logged on here since last July.  
You may want to take these factors into consideration when choosing a topic to respond to.

Side note: I recommend against using signals for this. There are a number of situations in which a signal does not get triggered, which means you still won’t have a profile model created.

That is why I recommend using the `get_or_create` function whenever the profile is needed. If the profile model exists, everything is ok. But if the profile model hasn’t yet been created, one will be.

---

<div class="post-metadata">

**Author:** ![guzmanojero](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/guzmanojero/32/1675_2.png) [@guzmanojero](https://forum.djangoproject.com/u/guzmanojero)\
**Post date:** [January 31, 2024, 5:26pm UTC](https://forum.djangoproject.com/t/profile-matching-query-does-not-exist/20079/11 "2024-01-31T17:26:16Z")

</div>

Thanks!

I just answer for the OP, but also for my future self and for others as well 😉

“Side note: I recommend against using signals for this. There are a number of situations in which a signal does not get triggered”

  
Thanks for the recommendation 💪
