Django数据库ManyToManyField

时间:2018-01-17 13:03:29

标签: python django database django-models many-to-many

您好我是从Django开始的,我创建了简单的数据库

class Voter(models.Model):
    STATUS_INACTIVE = 0
    STATUS_ACTIVE = 1
    STATUS_DELETED = 2

    Forename = models.CharField(max_length=50)
    Name = models.CharField(max_length=50)
    Degree = models.CharField(max_length=50, blank=True)
    Active = models.BooleanField(default=0)
    StatusChoices = (
        (STATUS_INACTIVE, 'Inactive'),
        (STATUS_ACTIVE, 'Active'),
        (STATUS_DELETED, 'Deleted'),
    )
    Status = models.IntegerField(choices=StatusChoices, default=STATUS_INACTIVE)
    ControllerId = models.OneToOneField(
        Controller,
        on_delete=models.CASCADE,
        blank=True
    )

    def __str__(self):
        return "%s %s" % (self.Forename, self.Name)

class AttendanceList(models.Model):
    Date = models.DateTimeField(null=True)
    Council = models.CharField(max_length=50)
    Attandence = models.ManyToManyField(
        Voter,
        through='Attendance',
    )

class Attendance(models.Model):
    ABSENT = 0
    PRESENT = 1

    Voter = models.ForeignKey(Voter, on_delete=models.CASCADE, db_index=True)
    AttendanceList = models.ForeignKey(AttendanceList, on_delete=models.CASCADE)

    Present = models.BooleanField(default=ABSENT)

当我试图通过管理员网站访问考勤表时,我得到例外:

  

(1054,“字段列表'中的未知栏'polls_attendance.Voter_id'”)

编辑:

我试图在Django中制作简单的投票应用程序。我以前从不使用任何框架,也没有用HTML或CSS做很多事情。我选择Django是因为Python。

这是我尝试访问此表时获得的。

回溯:

Environment:


Request Method: GET
Request URL: http://localhost:8000/admin/polls/attendance/

Django Version: 1.11.8
Python Version: 2.7.6
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'polls.apps.PollsConfig']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "/home/sch/PythonProjects/VirtualEnv/local/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
  41.             response = get_response(request)

File "/home/sch/PythonProjects/VirtualEnv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/home/sch/PythonProjects/VirtualEnv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/sch/PythonProjects/VirtualEnv/local/lib/python2.7/site-packages/django/contrib/admin/options.py" in wrapper
  551.                 return self.admin_site.admin_view(view)(*args, **kwargs)

File "/home/sch/PythonProjects/VirtualEnv/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  149.                     response = view_func(request, *args, **kwargs)

File "/home/sch/PythonProjects/VirtualEnv/local/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  57.         response = view_func(request, *args, **kwargs)

File "/home/sch/PythonProjects/VirtualEnv/local/lib/python2.7/site-packages/django/contrib/admin/sites.py" in inner
  224.             return view(request, *args, **kwargs)

File "/home/sch/PythonProjects/VirtualEnv/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapper
  67.             return bound_func(*args, **kwargs)

File "/home/sch/PythonProjects/VirtualEnv/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  149.                     response = view_func(request, *args, **kwargs)

File "/home/sch/PythonProjects/VirtualEnv/local/lib/python2.7/site-packages/django/utils/decorators.py" in bound_func
  63.                 return func.__get__(self, type(self))(*args2, **kwargs2)

File "/home/sch/PythonProjects/VirtualEnv/local/lib/python2.7/site-packages/django/contrib/admin/options.py" in changelist_view
  1662.             selection_note=_('0 of %(cnt)s selected') % {'cnt': len(cl.result_list)},

File "/home/sch/PythonProjects/VirtualEnv/local/lib/python2.7/site-packages/django/db/models/query.py" in __len__
  232.         self._fetch_all()

File "/home/sch/PythonProjects/VirtualEnv/local/lib/python2.7/site-packages/django/db/models/query.py" in _fetch_all
  1118.             self._result_cache = list(self._iterable_class(self))

File "/home/sch/PythonProjects/VirtualEnv/local/lib/python2.7/site-packages/django/db/models/query.py" in __iter__
  53.         results = compiler.execute_sql(chunked_fetch=self.chunked_fetch)

File "/home/sch/PythonProjects/VirtualEnv/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
  899.             raise original_exception

Exception Type: OperationalError at /admin/polls/attendance/
Exception Value: (1054, "Unknown column 'polls_attendance.Voter_id' in 'field list'")

0 个答案:

没有答案