Putra
Putra

Reputation: 431

Importing Django Custom User Model From Another App Model

So I have an app called profiles and it has model named Profile

from poros.abstracts.models import Model
from django.db import models
from poros.users.models import User


class Profile(Model):
    user = models.OneToOneField(
        to=User,
        on_delete=models.CASCADE,
        related_name='profile', )

the Profile model has a field name user which has OneToOneField relationship with my Django custom user User. The problem now is it returns me an error:

ImportError: cannot import name 'User'

How can I solve this issue?

Upvotes: 0

Views: 858

Answers (1)

Du D.
Du D.

Reputation: 5310

Don't refer to the user model directly, it is recommended by django by refer to the settings.AUTH_USER_MODEL.

Document

from django.conf import settings

class Profile(Model):
    user = models.OneToOneField(
        to=settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        related_name='profile', )

Upvotes: 2

Related Questions