Django How to add a User Foreignkey inside a User model?

I'm trying to insert a foreignkey of the User inside of the user model but I keep getting error. As as user I want to have two purposes, I can do i do a book to get my pets taken care of and also i can take care of other pets

class User(AbstractBaseUser, PermissionsMixin):
  name = models.CharField(max_length=255, default='John Doe')
  pet_sitter = models.ForeignKey('User',on_delete=models.CASCADE)//--->User inside User
  I tried get_user_model() it showed errors too//
  pet_appointments= models.ManyToManyField(
    through_fields=('patient', 'doctor')

class Booking(models.Model):
  start_date = models.DateTimeField()  
  end_date = models.DateTimeField()
  owner_of_pet = models.ForeignKey(get_user_model(),related_name = "User",on_delete=models.CASCADE, blank=True,null=True)
  sitter = models.ForeignKey('User', on_delete=models.CASCADE)


enter code here

django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model 'api.User' that has not been installed

Answers: 1

Answered by, Jan. 13, 2022, 5:50 a.m.
  1. Seems like you have defined some User in api app, so add that app to the installed apps in

Also, it is not really possible or needed to reference the User in this way.

I imagine your to be something like this:

from api.models import CustomUser
from django.db import models

class Pet(models.Model):
    name = models.CharField(max_length=100)
    pet_sitter = models.ForeignKey(CustomUser, on_delete=models.CASCADE)
    owner = models.ForeignKey(CustomUser, on_delete=models.CASCADE, related_name='pets', blank=True,null=True)

class Booking(models.Model):
    pet = models.ForeignKey(Pet, on_delete=models.CASCADE)
    start_date = models.DateTimeField()  
    end_date = models.DateTimeField()

Check the source code here: