django唯一约束失败salebook_saleman.user_id
model.py:
class Saleman(models.Model):
name = models.CharField(max_length=200)
password = models.CharField(max_length=200)
def __str__(self):
return self.name
class Book(models.Model):
owner = models.ForeignKey(Saleman)
book_name = models.CharField(max_length=200)
book_text = models.TextField(max_length=2000,blank=True)
price = models.IntegerField(default=0)
telephone = models.CharField(max_length=200)
#image = models.ImageField(upload_to='photos')
pub_date = models.DateTimeField('date published',default = timezone.now)
def __str__(self):
return self.book_name
view.py:
def user_register(request):
if request.method == 'POST':
username = request.POST['username']
password = request.POST['password']
email = request.POST['email']
new_user = User.objects.create_user(username,email,password)
new_saleman = Saleman.objects.create(name=username,password=password)
if newsaleman is not None:
new_saleman.save()
if new_user is not None:
new_user.save()
context = {'Username':username}
return render(request,"salebook/index.html",context)
else:
context = {'error_message':'Register Fail'}
return render(request,"salebook/register.html",context)
def user_login(request):
if request.method == 'POST':
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
context = {'Username':username}
return render(request,"salebook/index.html",context)
else:
context = {'error_message':'User is locked'}
return render(request,"salebook/login.html",context)
else:
context = {'error_message':'Null User or Wrong Password'}
return render(request,"salebook/login.html",context)
else:
return render_to_response('salebook/login.html')
不确定它的含义或原因是什么。 如果有一个简单的解决方法,请告诉我。谢谢! 更新: 错误是:
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 367, in execute_from_command_line
utility.execute()
File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Python27\lib\site-packages\django\core\management\base.py", line 294, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Python27\lib\site-packages\django\core\management\base.py", line 345, in execute
output = self.handle(*args, **options)
File "C:\Python27\lib\site-packages\django\core\management\commands\migrate.py", line 204, in handle
fake_initial=fake_initial,
File "C:\Python27\lib\site-packages\django\db\migrations\executor.py", line 115, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "C:\Python27\lib\site-packages\django\db\migrations\executor.py", line 145, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "C:\Python27\lib\site-packages\django\db\migrations\executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\Python27\lib\site-packages\django\db\migrations\migration.py", line 129, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "C:\Python27\lib\site-packages\django\db\migrations\operations\fields.py", line 84, in database_forwards
field,
File "C:\Python27\lib\site-packages\django\db\backends\sqlite3\schema.py", line 231, in add_field
self._remake_table(model, create_fields=[field])
File "C:\Python27\lib\site-packages\django\db\backends\sqlite3\schema.py", line 199, in _remake_table
self.quote_name(model._meta.db_table),
File "C:\Python27\lib\site-packages\django\db\backends\base\schema.py", line 112, in execute
cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "C:\Python27\lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\sqlite3\base.py", line 337, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: UNIQUE constraint failed:salebook_saleman.user_id
首先,我尝试使用abstractUser扩展我的模型销售人员,并且发生了这个错误。当我重做代码时,它仍然存在。很抱歉,我不知道我的代码发生了什么。
答案 0 :(得分:0)
看起来您的Saleman
模型曾经为用户模型提供了外键或一对一字段。您需要创建一个新的迁移,然后运行它以从数据库中删除该字段。
./manage.py makemigrations salebook
./manage.py migrate
答案 1 :(得分:0)
if newsaleman is not None:
new_saleman.save()