Reputation: 1
Good day, I'm currently on the middle of a practice project for django and can't seem to find a solution to a problem when creating an api with tastypie. I'm currently in the middle of learning so any help would be appreciated. I'm using django 4.0 and tastypie 2.8.2.
Here's the code which seems to be the problem:
from django.db import models
from tastypie.resources import ModelResource
Here's the full error I'm getting when attempting to execute python3 manage.py runserver on my venv:
> Exception in thread django-main-thread: Traceback (most recent call
> last): File "/usr/lib/python3.9/threading.py", line 954, in
> _bootstrap_inner
> self.run() File "/usr/lib/python3.9/threading.py", line 892, in run
> self._target(*self._args, **self._kwargs) File "/home/user/.local/share/virtualenv-path/lib/python3.9/site-packages/django/utils/autoreload.py",
> line 64, in wrapper
> fn(*args, **kwargs) File "/home/user/.local/share/virtualenv-path/lib/python3.9/site-packages/django/core/management/commands/runserver.py",
> line 115, in inner_run
> autoreload.raise_last_exception() File "/home/user/.local/share/virtualenv-path/lib/python3.9/site-packages/django/utils/autoreload.py",
> line 87, in raise_last_exception
> raise _exception[1] File "/home/user/.local/share/virtualenv-path/lib/python3.9/site-packages/django/core/management/__init__.py", line 381, in execute
> autoreload.check_errors(django.setup)() File "/home/user/.local/share/virtualenv-path/lib/python3.9/site-packages/django/utils/autoreload.py",
> line 64, in wrapper
> fn(*args, **kwargs) File "/home/user/.local/share/virtualenv-path/lib/python3.9/site-packages/django/__init__.py",
> line 24, in setup
> apps.populate(settings.INSTALLED_APPS) File "/home/user/.local/share/virtualenv-path/lib/python3.9/site-packages/django/apps/registry.py",
> line 114, in populate
> app_config.import_models() File "/home/user/.local/share/virtualenv-path/lib/python3.9/site-packages/django/apps/config.py",
> line 300, in import_models
> self.models_module = import_module(models_module_name) File "/usr/lib/python3.9/importlib/__init__.py", line 127, in import_module
> return _bootstrap._gcd_import(name[level:], package, level) File "frozen importlib._bootstrap", line 1030, in _gcd_import File
> "frozen importlib._bootstrap", line 1007, in _find_and_load File
> "frozen importlib._bootstrap", line 986, in _find_and_load_unlocked
> File "frozen importlib._bootstrap", line 680, in _load_unlocked File
> "frozen importlib._bootstrap_external", line 790, in exec_module
> File "frozen importlib._bootstrap", line 228, in
> _call_with_frames_removed **File "/home/user/myapp/api/models.py", line 2, in module**
> from tastypie.resources import ModelResource File "/home/user/.local/share/virtualenv-path/lib/python3.9/site-packages/tastypie/resources.py",
> line 13, in module
> from django.conf.urls import url ImportError: cannot import name 'url' from 'django.conf.urls'
> (/home/user/.local/share/virtualenv-path/lib/python3.9/site-packages/django/conf/urls/__init__.py)
After a bit of searching I found that some people managed to solve it by substituting "url" to "re_path" on the import however this doesn't seem to be working for me.
Here's how my resources.py file looks until the import error:
from __future__ import unicode_literals
from copy import copy, deepcopy
from datetime import datetime
import logging
import sys
from time import mktime
import traceback
import warnings
from wsgiref.handlers import format_date_time
from django.conf import settings
from django.conf.urls import url
The solution was apparently changing that last line to from django.conf.urls import re_path but the error I'm getting now is "from django.conf.urls import url ImportError: cannot import name 're_path' from 'django.conf.urls'". The problem seems to begin when I import ModelResource from the tastypie/resources directory so I checked the resources file for the ModelResource class and found it empty:
class ModelResource(six.with_metaclass(ModelDeclarativeMetaclass, BaseModelResource)):
pass
I'm not sure if that might have something to do though as this is how it was from installation. All packages were installed within the venv.
Again, I'm currently in the middle of learning so any assistance is deeply appreciated.
Upvotes: 0
Views: 255