'Visual Studio Code & Django: Error when importing User model

Edit: Still have this issue.

Visual Studio Code throws the following error:

User model imported from django.contrib.models

The error appears in line 2 of the following script (models.py). The code is from a Django tutorial and works fine. But it is annoying that Visual Studio Code (Pylint) throws an error (and marks the script red).

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

# Create your models here.


class Post(models.Model):
    title = models.CharField(max_length=100)
    content = models.TextField()
    date_posted = models.DateTimeField(default=timezone.now)
    author = models.ForeignKey(User, on_delete=models.CASCADE)

    def __str__(self):
        return self.title

Also VSC throws an error when importing a custom model, e.g. Post.

from app_blog.models import Post.

The error: Unable to import 'app_blog.models'

My setup:

  • Win10
  • Virtual Enviroment (Python, Django, Pylint, ...)
  • Django 3.1.1
  • Python 3.8.5
  • Pylint Django
  • VSC runs Python in my VENV

Pylint settings:

"python.linting.pylintArgs": [
        "--load-plugins=pylint_django",
        "--errors-only"
    ],
"python.linting.pylintUseMinimalCheckers": true

There are the following interpreter, Interpreter No. 2 is selected.

  1. Python 3.8.3 64-bit ('base': conda), ~\Anaconda3\python.exe
  2. Python 3.8.5 64-bit('venv'), .\venv\Scripts\python.exe
  3. Python 3.8.5 64-bit, ~\AppData\Local\Programs\Python\Python38\python.exe


Solution 1:[1]

This is late but you can always run the following in your terminal

pip install pylint-django

and add this to settings.json file found in .vscode folder (assuming you're using vscode)

 "python.linting.pylintArgs": [
    "--load-plugins",
    "pylint_django",
    "--errors-only",
    "--load-plugins pylint_django"
]

Hope this works for you!

Solution 2:[2]

For the first problem with the User, I've encountered a similar problem not sure if it's the same as yours, but I resolved it like this

  1. in models.py insted of from django.contrib.auth.models import User, I used

     from django.conf import settings
    
  2. in the line:

     author = models.ForeignKey(User, on_delete=models.CASCADE)"
    

    I replaced it with:

     author = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE)
    

    Accordind to the docs https://docs.djangoproject.com/en/3.0/topics/auth/customizing/#django.contrib.auth.get_user_model

Solution 3:[3]

I was having the same issue, and puzzled by the reason of it, I ended up opening a much clarifying thread on Django forum itself.

Here it is: https://forum.djangoproject.com/t/best-practice-and-documentation-from-django-contrib-auth-models-import-user-pylint-and-errors/12492

They are basically confirming that it's absolutely fine, if not encouraged, to use the from django.contrib.auth.models import User in your Django app.

Also, they clarified the means of the Django docs. The point will be probably extended further.

This ended on PyLint table as well, and they confirm (you can follow reading the sub-links in the discussion) that best way is to discharge the alert via settings or config files.

I believe this solve the issue once for all, and clarify when and how you should import User or get_user_model().

to the date of the 3rd of March 2022 the discussion is still open, so please follow along the links to have the latest updates, anyway.

Edit: following a suggestion coming straight from PyLint's contributors, I added to my local .pylintrc file:

# Customization
disable=imported-auth-user

worked like charm (finally!)

Solution 4:[4]

An alternative (when not using the User model in the models module but in other modules) is to use from django.contrib.auth import get_user_model

and use get_user_model() where you are now using User, this will probably give errors while you do this in the models.py module, because django isn't ready getting the user model yet, it can be changed in this module

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 paul iyinolu
Solution 2 Dharman
Solution 3
Solution 4 Jens Timmerman