From django db import models ошибка

This is weird. I can’t find the error. I can’t run the server (or anything) cause I get an error:

ImportError: cannot import name Libro

So these are the models:

perfiles.models.py-

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

from libros.models import Libro <- WEIRD ERROR ??¡?

class Perfil(models.Model):
    usuario = models.OneToOneField(User, null=True)
    actualmente_leyendo = models.ForeignKey(Libro, related_name="actualmente_leyendo")
    ...

libros.models.py —

from django.db import models

from perfiles.models import Perfil

    class Libro(models.Model):
        titulo = models.CharField(max_length=255, blank=True)
        autor = models.CharField(max_length=255, blank=True)
        imagen = models.CharField(max_length=255, blank=True)

So, both «libros» and «perfiles» are apps registered on my settings.py and , when I open a ´python manage.py shell´ and run ´from libros.models import Libro´, it works correctly and gives me

(InteractiveConsole)
>>> from libros.models import Libro
>>> Libro
<class 'libros.models.Libro'>

So, where could the error be? and why can the python shell import the model and the other model can’t? Any ideas will be helpfull. Thanks.

I am working on a project using django and I am using Visual Studio Code software.

In my ‘store’ directory i have a python package called ‘tiendaonline’ and the app called «gestionpedidos» where I am trying to create a a Table (DDBB)

The problem i am getting is that I cannot create table because when I try to run «py manage.py makemigrations» I can see the msg «No changes detected». Also I can see in the window called problems this msg: » Import «django.db.models» could not be resolved from source «

My setting.py is like this:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'gestionpedidos',
]

and my model is this:

from django.db import models

# Create your models here.
class Pedidos(models.Model):
    numero = models.IntegerField(max_length=100 )
    fecha = models.DateField(max_length=300 )
    entregado = models.BooleanField() 

class Clientes(models.Model):
    name = models.CharField(max_length=30 )
    direc = models.CharField(max_length=50 )
    Email = models.EmailField() 
    Tel = models.EmailField() 

class Articulos(models.Model):
    name = models.CharField(max_length=100 )
    seccion = models.CharField(max_length=300 )
    price = models.IntegerField() 

I don’t know what is happening. It could give me an adress of migration like «0001_init» but is not running.

Issue

I am working on a project using django and I am using Visual Studio Code software.

In my ‘store’ directory i have a python package called ‘tiendaonline’ and the app called «gestionpedidos» where I am trying to create a a Table (DDBB)

The problem i am getting is that I cannot create table because when I try to run «py manage.py makemigrations» I can see the msg «No changes detected». Also I can see in the window called problems this msg: » Import «django.db.models» could not be resolved from source «

My setting.py is like this:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'gestionpedidos',
]

and my model is this:

from django.db import models

# Create your models here.
class Pedidos(models.Model):
    numero = models.IntegerField(max_length=100 )
    fecha = models.DateField(max_length=300 )
    entregado = models.BooleanField() 

class Clientes(models.Model):
    name = models.CharField(max_length=30 )
    direc = models.CharField(max_length=50 )
    Email = models.EmailField() 
    Tel = models.EmailField() 

class Articulos(models.Model):
    name = models.CharField(max_length=100 )
    seccion = models.CharField(max_length=300 )
    price = models.IntegerField() 

I don’t know what is happening. It could give me an adress of migration like «0001_init» but is not running.

Solution

restart your vs code first and then activate your virtual environment if you are using. do again makemigrations and migrate if still no changes detected then delete recent migration and do it again

Answered By — I’mSRJ

The error «ImportError: cannot import name ‘FieldDoesNotExist’ from ‘django.db.models'» often occurs when running a Django management command such as «makemigrations» in a Django project. This error occurs because the FieldDoesNotExist class has been moved to a different module in the latest version of Django and the code that is being run is using an outdated reference to the class. The root cause of this error is an outdated import statement in the code that is being executed.

Method 1: Upgrade Django

To fix the «ImportError: cannot import name ‘FieldDoesNotExist’ from ‘django.db.models'» error while running «makemigrations» command on Django with Python 3.X, you can upgrade your Django version. Here are the steps to do it:

  1. Check your current Django version by running the following command in the terminal:
python -m django --version
  1. Upgrade your Django version by running the following command in the terminal:
pip install --upgrade django
  1. Check your Django version again to make sure it has been upgraded:
python -m django --version
  1. Now, try running the «makemigrations» command again. It should work without any errors.

Here is an example of how to upgrade Django version in a Django project’s virtual environment:

source path/to/venv/bin/activate

pip install --upgrade django

python -m django --version

That’s it! This should fix the «ImportError: cannot import name ‘FieldDoesNotExist’ from ‘django.db.models'» error while running «makemigrations» command on Django with Python 3.X.

Method 2: Use the Correct Import Statement

To fix the ImportError: cannot import name 'FieldDoesNotExist' from 'django.db.models' error when running python manage.py makemigrations, you can use the correct import statement in your Django project.

First, make sure you have Django version 2.2 or higher installed. Then, in your models.py file, replace the import statement:

from django.db.models.fields import FieldDoesNotExist

with:

from django.core.exceptions import FieldDoesNotExist

This will import the FieldDoesNotExist class from the correct module and fix the error.

Here’s an example of how your models.py file might look with the corrected import statement:

from django.db import models
from django.core.exceptions import FieldDoesNotExist

class MyModel(models.Model):
    my_field = models.CharField(max_length=50)

    def __str__(self):
        return self.my_field

    def get_field(self, field_name):
        try:
            return self._meta.get_field(field_name)
        except FieldDoesNotExist:
            return None

In this example, we’re using the FieldDoesNotExist class in the get_field method to catch and handle any errors that might occur when trying to get a field that doesn’t exist.

By using the correct import statement, you can avoid the ImportError and keep your Django project running smoothly.

Problem Description:

I am working on a project using django and I am using Visual Studio Code software.

In my ‘store’ directory i have a python package called ‘tiendaonline’ and the app called «gestionpedidos» where I am trying to create a a Table (DDBB)

The problem i am getting is that I cannot create table because when I try to run «py manage.py makemigrations» I can see the msg «No changes detected». Also I can see in the window called problems this msg: » Import «django.db.models» could not be resolved from source «

My setting.py is like this:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'gestionpedidos',
]

and my model is this:

from django.db import models

# Create your models here.
class Pedidos(models.Model):
    numero = models.IntegerField(max_length=100 )
    fecha = models.DateField(max_length=300 )
    entregado = models.BooleanField() 

class Clientes(models.Model):
    name = models.CharField(max_length=30 )
    direc = models.CharField(max_length=50 )
    Email = models.EmailField() 
    Tel = models.EmailField() 

class Articulos(models.Model):
    name = models.CharField(max_length=100 )
    seccion = models.CharField(max_length=300 )
    price = models.IntegerField() 

I don’t know what is happening. It could give me an adress of migration like «0001_init» but is not running.

Solution – 1

restart your vs code first and then activate your virtual environment if you are using. do again makemigrations and migrate if still no changes detected then delete recent migration and do it again

Solution – 2

If django is not detecting the changes do this
py manage.py makemigrations gestionpedidos to look for changes in that app only. I’m not sure why but this has worked for me countless times in the past.

Понравилась статья? Поделить с друзьями:
  • Forza horizon 5 ошибка загрузки
  • Fose loader fallout 3 ошибка
  • Forzahorizon5 exe ошибка приложения
  • From django conf urls import url ошибка
  • From crypto cipher import aes выдает ошибку