0

When i try to reset database (i created a new empty database)

i got this error when i run this command python manage.py makemigrations (or migrate)

    for pattern in self.url_patterns:
  File "django\utils\functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "django\urls\resolvers.py", line 589, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "django\utils\functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "django\urls\resolvers.py", line 582, in urlconf_module
    return import_module(self.urlconf_name)
  File \python\python39\lib\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 "project\urls.py", line 18, in <module>
    path('api/v2/', include('project.api.urls'), name='api'),
  File "django\urls\conf.py", line 34, in include
    urlconf_module = import_module(urlconf_module)
  File "python\python39\lib\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 "project\api\urls.py", line 3, in <module>
    from .views import *
  File "project\api\views.py", line 146, in <module>
    class SampleViewSet(viewsets.ModelViewSet):
  File "project\api\views.py", line 148, in SampleViewSet
    general = General.objects.get(is_active=True)
  File "django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "django\db\models\query.py", line 425, in get
    num = len(clone)
  File "django\db\models\query.py", line 269, in __len__
    self._fetch_all()
  File "django\db\models\query.py", line 1308, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "django\db\models\query.py", line 53, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "django\db\models\sql\compiler.py", line 1156, in execute_sql
    cursor.execute(sql, params)
  File "django\db\backends\utils.py", line 98, in execute
    return super().execute(sql, params)
  File "django\db\backends\utils.py", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "django\db\backends\utils.py", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "env\lib\site-packages\django\db\utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "env\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
        django.db.utils.ProgrammingError: HATA:  "app_general" object doesnt exist
    LINE 1: ...", "app_general"."field1" FROM "field_2...

views.py

class SampleViewSet(viewsets.ModelViewSet):
    general = General.objects.get(is_active=True)
    queryset = model.objects.filter(field=site_general.field)

i deleted all migration files in all apps (without __init__ ) and also .pyc files

is there any idea for this problem?

(edit i added Traceback)

4
  • @AbdulAzizBarkat class SampleViewSet(viewsets.ModelViewSet): general = General.objects.get(is_active=True) queryset = model.objects.filter(field=site_general.field) Commented May 28, 2021 at 13:15
  • @AbdulAzizBarkat i added Commented May 28, 2021 at 13:21
  • What is site_general in model.objects.filter(field=site_general.field)? Commented May 28, 2021 at 13:21
  • @AbdulAzizBarkat I do not think views.py will cause problems because I changed the database many times but now there is a problem and I did not change views.py and that view. Commented May 28, 2021 at 13:26

1 Answer 1

3

All the non-method attributes of a class get evaluated /executed during the declaration or definition of the class. Hence when the interpreter reaches your class SampleViewSet it tries to execute the line general = General.objects.get(is_active=True) but of course your database doesn't exist yet and since calling .get implies a database query you get an error.

In general any statement that might cause a database query should be either run by the app configs ready method, by some middleware / context processor, or by the view in a request. Looking at the code it doesn't look like you use general anywhere, so you can simply remove it:

class SampleViewSet(viewsets.ModelViewSet):
    queryset = model.objects.filter(field=site_general.field)

You might say model.objects.filter(...) would also cause a query, but .filter returns a QuerySet and queryset's are lazy and hence evaluated only when needed, so that will not cause any error.

Sign up to request clarification or add additional context in comments.

1 Comment

I got it on the comment line and it worked, but it was a strange experience. Thanks for help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.