@Saharman

Как сделать поле выбора с вариантами из связанной модели?

Вообщем, есть задача связать две модели связью много-ко-много. Есть модель Project и есть модель People. В проектах есть поле leader, которое я хочу реализовать так, чтобы при добавление проекта в админке, в поле лидер был выпадающий список, откуда можно было бы выбрать из всего списка людей. Я сделал следующий код, но он выдает ошибку, помогите разобраться:

class People(models.Model):
    firstName = models.CharField(max_length= 50)
    lastName = models.CharField(max_length = 50)
    email = models.EmailField()



    def __str__(self):
        return self.lastName + ' ' + self.firstName


class Project(models.Model):
    title = models.CharField(max_length = 100)
    text = models.TextField()
    small_img = models.ImageField(upload_to='style/static/img')
    big_img = models.ImageField(upload_to='style/static/img')
    description = models.TextField()
    users = models.ManyToManyField(People)
    leader = models.CharField(max_length= 100, choices= 
    People.objects.all())



    def __str__(self):
        return self.title


Ошибка:
$ python manage.py makemigrations
Traceback (most recent call last):
  File "/home/nafanya/.local/lib/python3.5/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/home/nafanya/.local/lib/python3.5/site-packages/django/db/backends/sqlite3/base.py", line 303, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: style_people

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/home/nafanya/.local/lib/python3.5/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "/home/nafanya/.local/lib/python3.5/site-packages/django/core/management/__init__.py", line 347, in execute
    django.setup()
  File "/home/nafanya/.local/lib/python3.5/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/nafanya/.local/lib/python3.5/site-packages/django/apps/registry.py", line 112, in populate
    app_config.import_models()
  File "/home/nafanya/.local/lib/python3.5/site-packages/django/apps/config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 985, in _gcd_import
  File "<frozen importlib._bootstrap>", line 968, in _find_and_load
  File "<frozen importlib._bootstrap>", line 957, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 697, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/home/nafanya/PycharmProjects/styleru/style/models.py", line 15, in <module>
    class Project(models.Model):
  File "/home/nafanya/PycharmProjects/styleru/style/models.py", line 23, in Project
    leader = models.CharField(max_length= 100, choices= list)
  File "/home/nafanya/.local/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 1042, in __init__
    super().__init__(*args, **kwargs)
  File "/home/nafanya/.local/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 157, in __init__
    self.choices = choices or []
  File "/home/nafanya/.local/lib/python3.5/site-packages/django/db/models/query.py", line 276, in __bool__
    self._fetch_all()
  File "/home/nafanya/.local/lib/python3.5/site-packages/django/db/models/query.py", line 1179, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/home/nafanya/.local/lib/python3.5/site-packages/django/db/models/query.py", line 53, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "/home/nafanya/.local/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 1066, in execute_sql
    cursor.execute(sql, params)
  File "/home/nafanya/.local/lib/python3.5/site-packages/django/db/backends/utils.py", line 100, in execute
    return super().execute(sql, params)
  File "/home/nafanya/.local/lib/python3.5/site-packages/django/db/backends/utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/home/nafanya/.local/lib/python3.5/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/home/nafanya/.local/lib/python3.5/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/home/nafanya/.local/lib/python3.5/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/nafanya/.local/lib/python3.5/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/home/nafanya/.local/lib/python3.5/site-packages/django/db/backends/sqlite3/base.py", line 303, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: style_people


$ python manage.py migrate
Traceback (most recent call last):
  File "/home/nafanya/.local/lib/python3.5/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/home/nafanya/.local/lib/python3.5/site-packages/django/db/backends/sqlite3/base.py", line 303, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: style_people

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/home/nafanya/.local/lib/python3.5/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "/home/nafanya/.local/lib/python3.5/site-packages/django/core/management/__init__.py", line 347, in execute
    django.setup()
  File "/home/nafanya/.local/lib/python3.5/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/nafanya/.local/lib/python3.5/site-packages/django/apps/registry.py", line 112, in populate
    app_config.import_models()
  File "/home/nafanya/.local/lib/python3.5/site-packages/django/apps/config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 985, in _gcd_import
  File "<frozen importlib._bootstrap>", line 968, in _find_and_load
  File "<frozen importlib._bootstrap>", line 957, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 697, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/home/nafanya/PycharmProjects/styleru/style/models.py", line 15, in <module>
    class Project(models.Model):
  File "/home/nafanya/PycharmProjects/styleru/style/models.py", line 23, in Project
    leader = models.CharField(max_length= 100, choices= list)
  File "/home/nafanya/.local/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 1042, in __init__
    super().__init__(*args, **kwargs)
  File "/home/nafanya/.local/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 157, in __init__
    self.choices = choices or []
  File "/home/nafanya/.local/lib/python3.5/site-packages/django/db/models/query.py", line 276, in __bool__
    self._fetch_all()
  File "/home/nafanya/.local/lib/python3.5/site-packages/django/db/models/query.py", line 1179, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/home/nafanya/.local/lib/python3.5/site-packages/django/db/models/query.py", line 53, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "/home/nafanya/.local/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 1066, in execute_sql
    cursor.execute(sql, params)
  File "/home/nafanya/.local/lib/python3.5/site-packages/django/db/backends/utils.py", line 100, in execute
    return super().execute(sql, params)
  File "/home/nafanya/.local/lib/python3.5/site-packages/django/db/backends/utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/home/nafanya/.local/lib/python3.5/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/home/nafanya/.local/lib/python3.5/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/home/nafanya/.local/lib/python3.5/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/nafanya/.local/lib/python3.5/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/home/nafanya/.local/lib/python3.5/site-packages/django/db/backends/sqlite3/base.py", line 303, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: style_people
  • Вопрос задан
  • 108 просмотров
Решения вопроса 1
TheDeadOne
@TheDeadOne
Седой и строгий
class Project(models.Model):
    title = models.CharField(max_length = 100)
    text = models.TextField()
    small_img = models.ImageField(upload_to='style/static/img')
    big_img = models.ImageField(upload_to='style/static/img')
    description = models.TextField()
    users = models.ManyToManyField(People)
    leader = models.ForeignKey(People, on_delete=models.CASCADE, related_name='+')
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через TM ID
Похожие вопросы