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)
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
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