S
S
Saharman2018-03-09 21:03:10
Django
Saharman, 2018-03-09 21:03:10

How to make a select box with options from a linked model?

In general, there is a task to connect two models with a many-to-many relationship. There is a Project model and there is a People model. Projects have a leader field, which I want to implement so that when adding a project in the admin panel, there is a drop-down list in the leader field, from where you can select from the entire list of people. I made the following code, but it throws an error, help me figure it out:

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

Mistake:
$ 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

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2018-03-09
@Saharman

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='+')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question