5

Image of Error Message in Neovim

I have been trying to configure my neovim for django development and everything seems fine except for this issue I am having with fields in metaclasses. the image provided gives a snapshot and the code is as follows:

class UserSerializer(serializers.ModelSerializer):                              
  snippets = serializers.PrimaryKeyRelatedField(many=True, queryset=Snippet.objects.all())

the linting error indicates that it cannot access member objects for the Snippet class.

I am using coc-pyright with default settings. I tried playing around with the settings by enabling pylint and installing pylint-django in my project as a dev dependency but that was unable to resolve the issue. How would I fix this issue? Does anyone have a recommended setup for Django development in nvim?

umar tung
  • 51
  • 3
  • I'm using the built-in LSP server (via [nvim-lsp-installer](https://github.com/williamboman/nvim-lsp-installer/)). I had the exact same problem you described until I installed `django-stubs`, so... if you were looking for an excuse to upgrade your setup to use the built-in LSP, perhaps the possibility that completion/linting will Just Work™ will motivate you. chris@machine has a really good series on YT, [*Neovim from Scratch*](https://youtube.com/playlist?list=PLhoH5vyxr6Qq41NFL4GvhFp-WLd5xzIzZ), that explains how to set this all up. – evadeflow May 14 '22 at 19:13

1 Answers1

5

I am using a similar setup, and I could resolve the issue you described by installing the django-stubs package.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Anjo
  • 145
  • 1
  • 11
  • Are you installing `django-stubs` in Python interpreter you are using when opening neovim? This does not change it for me as I still get errors like `Cannot access member "id" for type "YourModel"`. – mpiskore Jan 04 '22 at 16:59
  • Yes. Did you follow the documentation at https://github.com/typeddjango/django-stubs ? – Anjo Jan 09 '22 at 09:46
  • Yes, stubs are installed and `setup.cfg` is updated as described, still no luck. My initial confusion came from the fact that mypy is not a required package for pyright understanding django-stubs, if I understand this correctly? – mpiskore Jan 11 '22 at 12:56
  • There is a lot of mention of `mypy` around `django-stubs`. Had no idea what that is. But `django-stubs` works without requiring `mypy`. I did `pip install django-stubs` in my environment and the error on the `objects` went away. – name-andy Oct 30 '22 at 12:46
  • 1
    You can just install django-stubs. I had the same issue with neovim, but now it's working fine. Use: pip install django-stubs[compatible-mypy] – ProfKache Dec 12 '22 at 09:06