2

If anyone knows how to solve this, please don't hesitate to help. I've tried many hints, but so far this problem still remains. Too frustrating!

I just started a little project on Django because I want to test Django Rest Framework. I created a project called project and inside it an app called user.

After that, I created a directory called api inside user and then I created the files viewsets.py and serializers.py in order to test Django Rest Framework.

enter image description here

In urls.py I imported "UsuarioViewSet" class from viewsets.

from project.user.api.viewsets import UsuarioViewSet

enter image description here

When I run python manage.py runserver the problem bellow ocurrs

enter image description here

(I also tried only user.api.viewsets... but it says "unresolved reference")

In my settings.py I registered 'user' and 'rest_framework':

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

OS: Linux Mint | IDE: PyCharm (community) | Virtualenv: venv | Python: 3.6.6

Renzo Rodrigues
  • 553
  • 2
  • 9
  • 19
  • You should use `from user.api.viewsets import UsuarioViewSet` instead of `from project.user.api.viewsets import UsuarioViewSet` – Vidya Sagar Nov 03 '18 at 14:03
  • I've already tried this way. As I said in my post, when I use like this, it says "unresolved reference". – Renzo Rodrigues Nov 03 '18 at 14:05
  • Can you paste the traceback of unresolved reference, this seems to be some other problem apart from this import error. – Vidya Sagar Nov 03 '18 at 14:08
  • try this for the unresolved reference - [link](https://stackoverflow.com/questions/21236824/unresolved-reference-issue-in-pycharm) – fmakawa Nov 03 '18 at 14:11
  • Why is there no `__init__.py` in the user directory? (`user/__init__.py`)? – axm__ Nov 03 '18 at 14:11
  • Yes, your manage.py is outside your project dir. Your Django project structured is screwed up. Please read this for better understanding of Django project layout: https://stackoverflow.com/questions/22841764/best-practice-for-django-project-working-directory-structure – Vidya Sagar Nov 03 '18 at 14:11
  • "Unresolved reference 'user' less... (Ctrl+F1) Inspection info: This inspection detects names that should resolve but don't. Due to dynamic dispatch and duck typing, this is possible in a limited but useful number of cases. Top-level and class-level items are supported better than instance items." – Renzo Rodrigues Nov 03 '18 at 14:11
  • `__init__.py` in `user/` already exists. Look. – Renzo Rodrigues Nov 03 '18 at 14:17
  • YAY! Thanks @fmakawa This link really fixed my problem! I'm very greateful. Thanks to everyone who tried to help me! – Renzo Rodrigues Nov 03 '18 at 14:30

1 Answers1

1

As far as I know, this kind of thing happens because of migration file __init__.py. The main reason for this is when you write code and make changes in the migration file and then you remove some part from code for which a migrate file has been already created.

So a solution is just delete __init__.py from migration and make sure that there is no other import which is removed or changed.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Milan
  • 46
  • 7