Queryset to return all UserProfile objects

I have this user profile model that has a OneToOne relationship with the django’s User model.
I want to query all user profiles, UserProfile.objects.all() but I keep getting errors.

What could be the problem? Please help

Error:
return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such column: job_userprofile.id

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


class UserProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    location= models.CharField(max_length=45)

The most common cause for this error is that you have not yet done a makemigrations and migrate to create this new profile model.

See the docs for makemigrations and migrate

You can also use showmigrations after each step to verify that a migration has been created and that it has been executed.

Thank you @KenWhitesell for your reply. I really appreciate.

It was confusing because the model was migrated. I had to drop the database and start afresh. Now I’m wondering if it’s possible to write a method inside the UserProfile class as indicated below to return

class UserProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    location= models.CharField(max_length=45)

    # function goes here
def get_profiles():
        profiles = UserProfile.objects.all()
        return list(profiles.values())

My question is, how would I reference UserProfile from within the UserProfile class itself. Thanks in advance

Why would you want to refer to the UserProfile class from within a UserProfile instance?

Model methods are intended to return data based on that individual instance.

Manager methods are intended to work on querysets referencing the Model.

If you are more specific about what exactly you’re trying to achieve, we might be able to provide a more detailed and targeted answer.

I saw this interview question that asked for that. I am preparing myself for interview so I’m just getting myself ready. Here’s the question.

I am trying to figure the second part of the question

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


class UserProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    dob = models.DateField(verbose_name="Date of Birth")


class Job(models.Model):
    title = models.CharField(verbose_name="Title")
    description = models.TextField(verbose_name="Description")
    employee = models.ForeignKey(User, on_delete=models.CASCADE)


# Add any missing Django best practices to model definitions

# Please write a single queryset that will return all UserProfile objects
# annotated with associated job ids