0
votes

I have extended the Django User model as per the docs: https://docs.djangoproject.com/en/1.8/topics/auth/customizing/

models.py:

from django.db import models
from django.contrib.auth.models import User
class Onboarding(models.Model):
    user = models.OneToOneField(User)
    onboarding_status = models.SmallIntegerField(max_length=1, default=1)

views.py:

from django.shortcuts import render
from django.contrib.auth.models import User
from .models import Onboarding

def home(request):
    current_user = request.user
    u = User.objects.get(id=current_user.id)
    onboarding_status = u.onboarding.onboarding_status

    context = {
    }

    if onboarding_status == 1:
        return render(request, "onboarding/step_1.html", context)
    else:
        return render(request, "onboarding/step_2.html", context)

However, I get an error RelatedObjectDoesNotExist at /onboarding/. User has no onboarding.

How do I accurately reference the onboarding_status integer I have associated with the user from a view?

2

2 Answers

2
votes

Firstly, it is pointless to query User for the request.user ID. request.user is already a User: the value of u is exactly the same as the current_user value you started with.

Secondly, the error is telling you that this particular User has no related Onboarding instance. You need to create one, probably when you create the user.

0
votes

You have assigned onboarding_status = u.onboarding.onboarding_status but your context = {} is empty. Try context={'onboarding_status': onboarding_status}. And then in the template files, that is step_1.html or step_2.html, use {{ onboarding_status }} to call the variable.