简体   繁体   中英

Django migration fails 'QuerySet' object has no attribute 'objects'

I have a set of models:

class DebugConf(models.Model):
    is_setup = models.BooleanField(default=False)
    debug_setup_date = models.DateTimeField()

    def __str__(self):
        return self.is_setup


class Currency(models.Model):
    currency_name = models.CharField(max_length=100)
    currency_value_in_dollars = models.FloatField()
    currency_value_in_dollars_date = models.DateTimeField()

    def __str__(self):
        return self.currency_name


class User(models.Model):
    user_name = models.CharField(max_length=200)
    user_pass = models.CharField(max_length=200)
    join_date = models.DateTimeField()

    def __str__(self):
        return self.user_name


class Transaction(models.Model):
    transaction_type = models.CharField(max_length=200)
    transaction_amount = models.FloatField()
    transaction_date = models.DateTimeField()
    transaction_currency = models.ForeignKey(Currency, on_delete=models.CASCADE)
    transaction_users = models.ForeignKey(User, on_delete=models.CASCADE)

    def __str__(self):
        return self.headline

a project 'crypto' and an app 'manage_crypto_currency' nested into it:

The app's views contains some initialization code:

views.py:

if IS_DEBUG_MODE:
    print('[!!!INFO!!!] DEBUG MODE SET! USING GENERATED TABLES')
    init_debug_tables()

def init_debug_tables():
    # Check if debug table has already been initialized
    debug_conf = DebugConf.objects.all()
    if debug_conf.objects.exists():
        return

At the moment, the db is not initialized; when running:

python manage.py makemigrations manage_crypto_currency

in the root (project dir):

I get:

[!!!INFO!!!] DEBUG MODE SET! USING GENERATED TABLES
Traceback (most recent call last):
  File "manage.py", line 24, in <module>
    main()
  File "manage.py", line 20, in main
    execute_from_command_line(sys.argv)
  File "C:\projects\django\crypto-currency-board\venv\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "C:\projects\django\crypto-currency-board\venv\lib\site-packages\django\core\management\__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\projects\django\crypto-currency-board\venv\lib\site-packages\django\core\management\base.py", line 330, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\projects\django\crypto-currency-board\venv\lib\site-packages\django\core\management\base.py", line 368, in execute
    self.check()
  File "C:\projects\django\crypto-currency-board\venv\lib\site-packages\django\core\management\base.py", line 392, in check
    all_issues = checks.run_checks(
  File "C:\projects\django\crypto-currency-board\venv\lib\site-packages\django\core\checks\registry.py", line 70, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
  File "C:\projects\django\crypto-currency-board\venv\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "C:\projects\django\crypto-currency-board\venv\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
    return check_method()
  File "C:\projects\django\crypto-currency-board\venv\lib\site-packages\django\urls\resolvers.py", line 408, in check
    for pattern in self.url_patterns:
  File "C:\projects\django\crypto-currency-board\venv\lib\site-packages\django\utils\functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\projects\django\crypto-currency-board\venv\lib\site-packages\django\urls\resolvers.py", line 589, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "C:\projects\django\crypto-currency-board\venv\lib\site-packages\django\utils\functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\projects\django\crypto-currency-board\venv\lib\site-packages\django\urls\resolvers.py", line 582, in urlconf_module
    return import_module(self.urlconf_name)
  File "C:\Users\msebi\AppData\Local\Programs\Python\Python38-32\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\projects\django\crypto-currency-board\crypto\crypto\urls.py", line 21, in <module>
    path('', include('manage_crypto_currency.urls')),
  File "C:\projects\django\crypto-currency-board\venv\lib\site-packages\django\urls\conf.py", line 34, in include
    urlconf_module = import_module(urlconf_module)
  File "C:\Users\msebi\AppData\Local\Programs\Python\Python38-32\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\projects\django\crypto-currency-board\crypto\manage_crypto_currency\urls.py", line 5, in <module>
    from . import views
  File "C:\projects\django\crypto-currency-board\crypto\manage_crypto_currency\views.py", line 24, in <module>
    init_debug_tables()
  File "C:\projects\django\crypto-currency-board\crypto\manage_crypto_currency\setup_test_db\setup_debug_tables.py", line 14, in init_debug_tables
    if debug_conf.objects.exists():
AttributeError: 'QuerySet' object has no attribute 'objects'

The line:

[!!!INFO!!!] DEBUG MODE SET! USING GENERATED TABLES

is in views. Apparently, the migrations tool feels the need to execute views.py. Why on Earth, I have no clue. Is it possible to make makemigrations NOT execute code that it shouldn't, eg just create the models given that the db is not initialized?

EDIT

The answers don't answer WHY migrate runs views.py (I don't know either), but commenting out:

# if IS_DEBUG_MODE:
#     print('[!!!INFO!!!] DEBUG MODE SET! USING GENERATED TABLES')
#     init_debug_tables()

leads to successful migration. Is there a less cumbersome way to go about it? (Un)comment is ugly.

You don't need to use call the objects of a QuerySet

def init_debug_tables():
    # Check if debug table has already been initialized
    debug_conf = DebugConf.objects.all()
    
        return

As the error message suggest, debug_conf is the the model class , it is an instance of a QuerySet . objects is the Manager attached to a model class. This managers creates QuerySet for you (in your code, by the return value of the all method).

So you want to do this:

def init_debug_tables():
    # Check if debug table has already been initialized
    debug_conf = DebugConf.objects.all()
    if debug_conf.exists():
        return

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM