Home > Net >  Django models ImportError
Django models ImportError

Time:03-14

I'm trying to create a Django 4 project. I added a new field in modules and when performing migrations. I have a mistake:

from ..users.models import Profile ImportError: attempted relative import beyond top-level package

i have next structure of the project:

django/devsearch/devseach/
                /project/__init.py
                |       /admin.py
                |       /apps.py
                |       /forms.py
                |       /models.py
                |       /test.py
                |       /urls.py
                |       /views.py
                /static
                /users/__init.py
                      /admin.py
                      /apps.py
                      /forms.py
                      /models.py
                      /test.py
                      /urls.py
                      /views.py

My files models.py: /project/models.py

from django.db import models
import uuid
from ..users.models import Profile


class Project(models.Model):
    owner = models.ForeignKey(Profile, null=True, blank=True, on_delete=models.SET_NULL)
    title = models.CharField(max_length=200)
    description = models.TextField(null=True, blank=True)
    featured_image = models.ImageField(null=True, blank=True, default='default.jpg')
    id = models.UUIDField(default=uuid.uuid4, unique=True,
                          primary_key=True, editable=False)
    demo_link = models.CharField(max_length=2000, null=True, blank=True)
    source_link = models.CharField(max_length=2000, null=True, blank=True)
    tags = models.ManyToManyField('Tag', blank=True)
    vote_total = models.IntegerField(default=0, null=True, blank=True)
    vote_ratio = models.IntegerField(default=0, null=True, blank=True)
    created = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.title

/users/models.py

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


class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, null=True, blank=True)
    name = models.CharField(max_length=200, blank=True, null=True)
    username = models.CharField(max_length=200, blank=True, null=True)
    email = models.EmailField(max_length=500, blank=True, null=True)
    short_info = models.CharField(max_length=200, blank=True, null=True)
    bio = models.TextField(max_length=2000, blank=True, null=True)
    profile_image = models.ImageField(
        blank=True, null=True, upload_to='profiles/', default='profiles/user-default.png')
    social_github = models.CharField(max_length=200, blank=True, null=True)
    social_twitter = models.CharField(max_length=200, blank=True, null=True)
    social_youtube = models.CharField(max_length=200, blank=True, null=True)
    social_linkedin = models.CharField(max_length=200, blank=True, null=True)
    social_website = models.CharField(max_length=200, blank=True, null=True)
    created = models.DateTimeField(auto_now_add=True)
    id = models.UUIDField(default=uuid.uuid4, unique=True,
                          primary_key=True, editable=False)

How i can import users models in project?

CodePudding user response:

from users.models import Profile

instead of from

..users.models import Profile
  • Related