Raise self.model.DoesNotExist( users.models.Profile.DoesNotExist: Profile matching query does not exist

views.py

from django.shortcuts import render, redirect
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from .forms import UserRegisterForm, UserUpdateForm, ProfileUpdateForm, Profile


def register(request):
    if request.method == 'POST':
        form = UserRegisterForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data.get('username')
            messages.success(
                request, "Your account has been created! Your ar now able to login.")
            return redirect('login')
    else:
        form = UserRegisterForm()
    return render(request, 'users/register.html', {'form': form})


@login_required
def profile(request):
    if request.method == 'POST':
        u_form = UserUpdateForm(request.POST, instance=request.user)
        # p_form = ProfileUpdateForm(
        #     request.POST, request.FILES, instance=request.user.profile)
        profile = Profile.objects.get(user = request.user)
        p_form = ProfileUpdateForm(request.POST, request.FILES, instance = profile)
        if u_form.is_valid() and p_form.is_valid():
            u_form.save()
            p_form.save()
            messages.success(request, "Your account has been updated!")
            return redirect('profile')

    else:
        u_form = UserUpdateForm(instance=request.user)
        profile = Profile.objects.get(user = request.user)
        p_form = ProfileUpdateForm(request.POST, request.FILES, instance = profile)
    context = {
        'u_form': u_form,
        'p_form': p_form
    }
    return render(request, 'users/profile.html', context)

signals.py

from django.db.models.signals import post_save
from django.dispatch import receiver
from .models import CustomUser


@receiver(post_save, sender=CustomUser)
def create_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(email=instance)


@receiver(post_save, sender=CustomUser)
def save_profile(sender, instance, **kwargs):
    instance.profile.save()

models.py

from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin
from django.db import models
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from django.db import models
from django.contrib.auth.models import User
from PIL import Image


from .managers import CustomUserManager

from django.contrib import admin





class CustomUser(AbstractBaseUser, PermissionsMixin):
    name = models.CharField(max_length=200)
    email = models.EmailField(_('email address'), unique=True)
    registration = models.IntegerField(_('registration'), unique=True)
    is_staff = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True)
    date_joined = models.DateTimeField(default=timezone.now)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    objects = CustomUserManager()

    def __str__(self):
        return self.email



class Profile(models.Model):
    user = models.OneToOneField(CustomUser, on_delete=models.CASCADE)
    image = models.ImageField(default='default.jpg', upload_to='profile_pics')

forms.py

from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from .models import Profile
from .models import CustomUser
from django import forms


class UserRegisterForm(UserCreationForm):
    email = forms.EmailField()

    class Meta:
        model = CustomUser
        fields = ['name', 'email','registration', 'password1', 'password2']

class UserUpdateForm(forms.ModelForm):
    email = forms.EmailField()

    class Meta:
        model = CustomUser
        fields = ['name', 'email','registration']

class ProfileUpdateForm(forms.ModelForm):
    class Meta:
        model = Profile
        fields = ['image']

admin.py

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin

from .models import Profile
from .models import CustomUser



class ProfileAdmin(admin.ModelAdmin):
    list_display = ('id', 'user')
    list_display_links = ('id', 'user')
    list_filter = ('user', )
    list_per_page = 20

admin.site.register(Profile, ProfileAdmin)

Here I'm extending User Model using AbstractBaseUser. I want to add a new Profile. and I'm adding the Profile Model with CustomUser model using OneToOneField. My register and login is working. But can't see the profiles and it gives error But I'm facing the above problem please help me!!

Solved.

I was having trouble with Profile.objects.create in my signals.py It was imported from apps.py. and in settings.py installed apps I was including my appname directly. by writing 'users'. changing users to 'users.apps.UsersConfig' solved the bug.

Back to Top