Django update model when form previously wasn't filled

I'm building a matrimonial website in Django. I have multiple forms that users need to fill in. Forms are distributed across pages.

There's an 'Edit Profile' option that I have added that can be accessed after the user has logged in. The problem is that if a user doesn't fill out a form for some reason, I face an exception when I try to get the said user's profile data.

UserProfile.objects.get(user_model_for_profile = user)

What do I do?

What you’re running into is a very common issue in multi-step profile systems.

The root cause is usually that some related profile objects/forms do not exist yet for a user, but your code assumes they always do.

For example, if you have models like:

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

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

and a user only completes BasicDetails but skips EducationDetails, then this will raise an exception:

request.user.educationdetails

because the related object does not exist.

A clean Django approach is:

1. Use get_or_create()

Whenever loading editable profile sections:

education, created = EducationDetails.objects.get_or_create(
    user=request.user
)

This guarantees an object always exists.


2. Handle missing related objects safely

Instead of directly accessing reverse OneToOne relations:

request.user.educationdetails

wrap it safely:

try:
    education = request.user.educationdetails
except EducationDetails.DoesNotExist:
    education = None

or better:

education = getattr(request.user, 'educationdetails', None)

3. Consider a profile completion workflow

In matrimonial platforms, users rarely complete everything in one sitting. A better UX is:

  • Save each section independently

  • Show profile completion percentage

  • Allow incomplete profiles

  • Mark missing sections visually

Example:

completion = 0

if hasattr(user, 'basicdetails'):
    completion += 25

if hasattr(user, 'educationdetails'):
    completion += 25

4. Pre-create related profile models during signup

A more scalable solution is creating empty profile sections automatically using signals.

Example:

@receiver(post_save, sender=User)
def create_profile_sections(sender, instance, created, **kwargs):
    if created:
        BasicDetails.objects.create(user=instance)
        EducationDetails.objects.create(user=instance)

Then forms simply update existing objects instead of creating them later.


5. Best practice recommendation

For matrimonial sites specifically, I’d recommend:

  • One central Profile model

  • Additional optional section models

  • Auto-create mandatory related objects

  • Allow nullable/blank fields

  • Use progressive completion instead of forcing all forms

This avoids exceptions and gives users a smoother experience.

We're missing some background, but - your profile and your account are separated objects? There are several options, really.

  • Force user to fill in profile during account setup

  • Do you need the profile at the point that you're getting the error? If not, don't depend on it.

  • Catch the error and surface the problem to the user. Alert that profile is needed. Or force redirect to profile. get() throws errors for DoesNotExist and ManyObjectsReturned (off top of head, verify) - these are simple to catch.

  • Merge necessary aspects of profile into user account object, and remove profile dependency.

Вернуться на верх