所以我一直在尝试创建一个人员列表,这是现在允许的其他人,查看和修改在通用Django应用程序中创建的项目。问题是,在创建项目并将创建项目的用户添加到staff_list字段时,我收到错误
OperationalError at /projects/create/
no such column: projectManager_project_staff_list.project_id
我的项目模型:
from django.db import models
from django.conf import settings
from folderManager.models import Folder
from django.contrib.contenttypes.models import ContentType
from django.db.models.signals import pre_save
from django.utils.text import slugify
from django.core.urlresolvers import reverse
from trackManager.models import Track
from django.contrib.auth.models import User
class Project (models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1) #the author
staff_list = models.ManyToManyField(User, related_name = "Staff")
title = models.CharField(max_length = 100)
slug = models.SlugField(unique=True)
description = models.TextField()
updated = models.DateTimeField(auto_now=True, auto_now_add=False)
timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse("projects:project_detail", kwargs={"slug": self.slug})
class Meta:
ordering = ["-timestamp", "-updated"]
@property
def tracks(self):
instance = self
qs = Track.objects.filter_by_instance(instance)
return qs
@property
def get_content_type(self):
instance = self
content_type = ContentType.objects.get_for_model(instance.__class__)
return content_type
我的项目创建视图:
def project_create(request):
form = ProjectForm(request.POST or None)
user = User.objects.get(username=request.user.username)
'''
tried to see if using a defined user variable instead of request.user would help
'''
if form.is_valid():
title_text = form.cleaned_data.get('title')
description_data = form.cleaned_data.get('description')
instance = form.save(commit=False)
instance.save()
new_project = Project.objects.get_or_create(
title = title_text,
description = description_data
)
newest_project = new_project[0]
newest_project.staff_list.add(user)
newest_project.save()
#messages.success(request, "Successfully Created")
return HttpResponseRedirect(instance.get_absolute_url())
context = {
"form": form,
}
return render(request, "project_form/project_form.html", context)
最新迁移:
# -*- coding: utf-8 -*-
# Generated by Django 1.11 on 2017-05-09 11:58
from __future__ import unicode_literals
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name='Project',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=100)),
('slug', models.SlugField(unique=True)),
('description', models.TextField()),
('updated', models.DateTimeField(auto_now=True)),
('timestamp', models.DateTimeField(auto_now_add=True)),
('staff_list', models.ManyToManyField(related_name='Staff', to=settings.AUTH_USER_MODEL)),
('user', models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
options={
'ordering': ['-timestamp', '-updated'],
},
),
]