Home > database >  How can i solve this error? I'm working with django on a school project and i'm stuck
How can i solve this error? I'm working with django on a school project and i'm stuck

Time:06-14

i get this error " ModuleNotFoundError: No module named 'models' ". I created a model called Job and I imported it to the admin. But I have that error. I've tried switching up the names of the model and some other folder on the project cause I read somewhere that could be the issue but I can't seem to get my head around it.

from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone

class Job(models.Model):

    title = models.CharField(max_length=150)
    company = models.CharField(max_length=100)
    location = models.CharField(max_length=200)
    salary = models.CharField(max_length=100)
    nature = models.CharField(max_length=10, choices=TYPE_CHOICES, default=FULL_TIME)
    experience = models.CharField(max_length=10, choices=EXP_CHOICES, default=TIER1)
    summary = models.TextField()
    description = models.TextField()
    requirements = models.TextField()
    logo = models.ImageField (default='default.png', upload_to='upload_images')
    date_created = models.DateTimeField(default=timezone.now)
    owner = models.ForeignKey(User, on_delete=models.CASCADE)


    def __str__(self):
        return '{} looking for {}'.format(self.company, self.title) 

then this is my input in the admin.py file

from django.contrib import admin
from models import Job

admin.site.register(Job)

CodePudding user response:

You are getting this error because models is not a module. Use this instead.

from django.contrib import admin
from .models import Job

admin.site.register(Job)

What the '.' does is find a models file in the directory the admin.py is in.

Hope this helps. Keep learning.

CodePudding user response:

Add a . before models in your import in admin.py to specify that you want to look in the same directory, like so :

from .models import Job
  • Related