A
A
alex_deerk2016-07-07 13:43:33
Django
alex_deerk, 2016-07-07 13:43:33

Django migration error. What is the problem?

I didn't change anything in the settings. Just wrote a model class and tried to execute:

py manage.py makemigrations

import PIL
from django.db import models
available = (('is', "В наличии"), ('c', "Под заказ"))
class Phone(models.Model):
    name = models.CharField(max_length=100, verbose_name='Название', help_text='Введите название', default='')
    availability = models.CharField(max_length=100, verbose_name='Наличие', choices=available, default=available[0])
    photo = models.ImageField(name='1', upload_to='/{0}/'.format(name))

D:\Projects\elektroswit>py manage.py makemigrations
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\management\__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\management\__init__.py", line 327, in execute
    django.setup()
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\apps\registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\apps\config.py", line 202, in import_models
    self.models_module = import_module(models_module_name)
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python35-32\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 662, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "D:\Projects\elektroswit\shop\models.py", line 3, in <module>
    class Phone(models.Model):
  File "D:\Projects\elektroswit\shop\models.py", line 6, in Phone
    photo = models.ImageField(name='1', upload_to='/{0}/'.format(name))
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\models\fields\__init__.py", line 188, in __str__
    model = self.model
AttributeError: 'CharField' object has no attribute 'model'

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sim3x, 2016-07-07
@alex_deerk

https://docs.djangoproject.com/en/1.9/ref/models/f...
https://stackoverflow.com/questions/28205560/uploa...

M
Maxim Vasiliev, 2016-07-07
@qmax

upload_to='/{0}/'.format(name)
Calculated during class initialization. The variable name is not defined at this point, and an exception occurs.
Python is trying to throw this exception at you, but it can’t say anything, because neither the field nor the class itself has been initialized yet (not created at all).
To customize this parameter, you need to put a function there.
Read the documentation here
https://docs.djangoproject.com/en/1.9/ref/models/f...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question