Raiz Ahmed
Raiz Ahmed

Reputation: 1

from models import Category ModuleNotFoundError: No module named 'models' using Django

settings.py:

from django.apps import AppConfig
class CategoryConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'api.category'

models.py:

from django.db import models


class Category(models.Model):
    name = models.CharField(max_length=50)
    description = models.CharField(max_length=250)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

admin.py code:

from django.contrib import admin
from models import Category
admin.site.register(Category)

apps.py:

from django.apps import AppConfig
class CategoryConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'api.category'

Error in the terminal:

File "/home/kickme/Desktop/lcodev/ecom/api/category/admin.py", line 2, in from models import Category ModuleNotFoundError: No module named 'models'

Upvotes: 0

Views: 2527

Answers (1)

Faisal Nazik
Faisal Nazik

Reputation: 2873

In the admin.py

from django.contrib import admin

from models import Category #this line is responsible for the break

admin.site.register(Category)

If your models.py ss in the same directory then use

from .models import Category

This means to import the model Category from the models.py file in the same directory

Upvotes: 1

Related Questions