# Error: DoesNotExist: matching query does not exist.

**URL:** <https://forum.djangoproject.com/t/error-doesnotexist-matching-query-does-not-exist/9096>\
**Category:** Using Django\
**Created:** [August 5, 2021, 4:19am UTC](https://forum.djangoproject.com/t/error-doesnotexist-matching-query-does-not-exist/9096 "2021-08-05T04:19:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![GoAmeer030](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/goameer030/32/5316_2.png) [@GoAmeer030](https://forum.djangoproject.com/u/GoAmeer030)\
**Post date:** [August 5, 2021, 4:19am UTC](https://forum.djangoproject.com/t/error-doesnotexist-matching-query-does-not-exist/9096/1 "2021-08-05T04:19:38Z")

</div>

I want to save user details from a **custom form** which has **OneToOne** connection with **User Model**. While i try to save it I am getting this error.

> UserAuthentication matching query does not exist

The below line is the line from which i getting the above error

> `userauth_obj = UserAuthentication.objects.get(U_User=user_obj)`

**views.py**

```auto
@login_required(login_url='login')
def UserAuthform(request):

    if request.method == 'POST':

        user = request.user.username
        auth = request.POST.get('D_Auth')
        chid = request.POST.get('D_ChID')
        utyp = request.POST.get('U_Type')
        loss = request.POST.get('N_Loss')

        if auth is None:
            messages.success(request, 'Authentication Key Must be Entered')
            return redirect('UserAuthForm')

        elif chid is None:
            messages.success(request, 'Channel ID Must be Entered')
            return redirect('UserAuthForm')

        elif UserAuthentication.objects.filter(D_Auth=auth).exists():
            messages.success(request, 'Authentication key in Use')
            return redirect('UserAuthForm')

        elif utyp is None:
            messages.success(request, 'User Type Must Be Selected')
            return redirect('UserAuthForm')

        else:
            user_obj = User.objects.get(username=user)

            userauth_obj = UserAuthentication.objects.get(U_User=user_obj)

            userauth_obj.D_Auth = auth
            userauth_obj.D_ChID = chid
            userauth_obj.U_Type = utyp
            userauth_obj.N_Loss = loss
            userauth_obj.save()

            return redirect('Home')

    return render(request, 'registration/userauthform.html')

```

**models.py**

```auto
class UserAuthentication(models.Model):
    U_User = models.OneToOneField(
        User,
        on_delete=models.CASCADE,
        primary_key= True,
    )

    D_Auth = models.CharField(max_length=100, unique=True)
    D_ChID = models.PositiveIntegerField(null=False)
    U_Type = models.CharField(max_length=7)
    N_Loss = models.BooleanField(null=False, default=False)
    U_PreM = models.BooleanField(null=False, default=False)

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

```

**userauthform.html**

```auto
{% extends "index.html" %}
{% block content %}

<div class="container">
        <form method="post" class="form" id="form">
        {% csrf_token %}

        <h1>Register</h1>

        {% if messages %}
            {% for message in messages %}
                <div class="alert alert-info">
                    {{ message }}
                </div>
            {% endfor %}
        {% endif %}

        <div class="form-group">
            <label for="D_Auth">Discord Authentication Key</label>
            <input type="text" name="D_Auth" id="D_Auth" class="form-control">
        </div>

        <div class="form-group">
            <label for="D_ChID">Discord Channel Id</label>
            <input type="text" name="D_ChID" id="D_ChID" class="form-control">
        </div>

        <div class="form-group">
            <label for="U_Type">Player Type</label>
            <select name="U_Type" id="U_Type" class="form-control">
                <option value="Normal">Normal Player</option>
                <option value="Patron">Patron Player</option>
            </select>
        </div>

        <div class="form-group">
            <label for="N_Loss">No Loss</label>
            <input type="checkbox" name="N_Loss" id="N_Loss" class="form-control">
        </div>

        <button class="btn btn-success mt-3" type="submit">Submit</button>

        <a href="{% url 'register' %}">Create a Account</a>
        <a href="{% url 'forgetpass' %}">Reset My Password</a>

        </form>
</div>

{% endblock content %}

```

---

<div class="post-metadata">

**Author:** ![CodenameTim](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/codenametim/32/31507_2.png) [@CodenameTim](https://forum.djangoproject.com/u/CodenameTim)\
**Post date:** [August 5, 2021, 2:32pm UTC](https://forum.djangoproject.com/t/error-doesnotexist-matching-query-does-not-exist/9096/2 "2021-08-05T14:32:37Z")

</div>

Hi @GoAmeer030, the clue is in the error message. `UserAuthentication matching query does not exist`. The query is `.get(U_User=user_obj)`. Breaking it down it means that there is no UserAuthentication instance that has the propert `U_User` set to the `user_obj` instance. This likely means that a user was created, but the corresponding `UserAuthentication` was not. I’m not entirely sure of the purpose of this view, but you could handle it with [`update_or_create`](https://docs.djangoproject.com/en/3.2/ref/models/querysets/#update-or-create):

```auto
userauth_obj, created = UserAuthentication.objects.update_or_create(
     U_User=user_obj,
     defaults={
            "D_Auth": auth,
            "D_ChID": chid,
            "U_Typ": utyp,
            "N_Loss": loss,
     }
)

```

---

<div class="post-metadata">

**Author:** ![GoAmeer030](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/goameer030/32/5316_2.png) [@GoAmeer030](https://forum.djangoproject.com/u/GoAmeer030)\
**Post date:** [August 6, 2021, 5:05am UTC](https://forum.djangoproject.com/t/error-doesnotexist-matching-query-does-not-exist/9096/3 "2021-08-06T05:05:02Z")

</div>

Thanks bro it works perfectly fine. Please take a look on this too [Link](https://forum.djangoproject.com/t/how-to-overwrite-on-my-django-models/9056/7). And Please tell where you find these functions and their uses. Thankyou

---

<div class="post-metadata">

**Author:** ![CodenameTim](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/codenametim/32/31507_2.png) [@CodenameTim](https://forum.djangoproject.com/u/CodenameTim)\
**Post date:** [August 6, 2021, 1:35pm UTC](https://forum.djangoproject.com/t/error-doesnotexist-matching-query-does-not-exist/9096/4 "2021-08-06T13:35:32Z")

</div>

> [@GoAmeer030](#):
>
> Please tell where you find these functions and their uses.

For me it’s been reading the docs occasionally combined with repeated usage of the functions that caused me to remember that they exist.
