0
from django.contrib.auth import get_user_model
from django.contrib.auth.models import AbstractUser

class MyUser(AbstractUser):
    pass

class Landlord(models.Model):
    user = models.OneToOneField(get_user_model(), on_delete=models.CASCADE)
    #other fields
    def __str__(self):
        # **Error is here**
        return self.user.email

When I use email field of my user it has give this error: "Instance of 'OneToOneField' has no 'email' member"

what is the reason for error?(And what fields are there in AbstractUser class?) How can I fix the error.

Jaha
  • 3
  • 2

3 Answers3

0

You should ensure you specify your user class in the settings.py file like

AUTH_USER_MODEL = 'appname.MyUser'

Replace appname with the name of your app.

Or you use MyUser instead of get_user_model().

HAKS
  • 419
  • 4
  • 9
0

I am using AbstractBaseUser instead of AbstractUser and the problem didn't come up. also by reading the code of django.contrib.auth.models you can see what fields have been implemented and how. it can be done by holding the control key and clicking on the import address.

  • Thank you! It seems explicitly creating email field after inheriting from AbstractBaseUser is the one I finally got no problem with custom user. – Jaha May 05 '20 at 07:35
0

Maybe...

from django.contrib.auth import models
from django.contrib.auth.models import AbstractUser

class Landlord(AbstractUser):
    email = models.EmailField(max_length=100, unique=True) 
    #other fields
    def __str__(self):
        return self.email
Harley
  • 1,305
  • 1
  • 13
  • 28