我正在尝试在Django中创建一个跟随器系统,并希望使用外键来实现。我输入了以下代码,但出现错误提示
File "D:\grayocean\manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\Mustafa Lakhani\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:\Users\Mustafa Lakhani\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\Mustafa Lakhani\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\Mustafa Lakhani\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 353, in execute
output = self.handle(*args, **options)
File "C:\Users\Mustafa Lakhani\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "C:\Users\Mustafa Lakhani\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\commands\migrate.py", line 203, in handle
fake_initial=fake_initial,
File "C:\Users\Mustafa Lakhani\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "C:\Users\Mustafa Lakhani\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\executor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "C:\Users\Mustafa Lakhani\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\Users\Mustafa Lakhani\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "C:\Users\Mustafa Lakhani\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\operations\fields.py", line 84, in database_forwards
field,
File "C:\Users\Mustafa Lakhani\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\sqlite3\schema.py", line 309, in add_field
self._remake_table(model, create_field=field)
File "C:\Users\Mustafa Lakhani\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\sqlite3\schema.py", line 181, in _remake_table
self.effective_default(create_field)
File "C:\Users\Mustafa Lakhani\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\schema.py", line 239, in effective_default
return field.get_db_prep_save(default, self.connection)
File "C:\Users\Mustafa Lakhani\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\fields\related.py", line 937, in get_db_prep_save
return self.target_field.get_db_prep_save(value, connection=connection)
File "C:\Users\Mustafa Lakhani\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\fields\__init__.py", line 790, in get_db_prep_save
return self.get_db_prep_value(value, connection=connection, prepared=False)
File "C:\Users\Mustafa Lakhani\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\fields\__init__.py", line 956, in get_db_prep_value
value = self.get_prep_value(value)
File "C:\Users\Mustafa Lakhani\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\fields\__init__.py", line 965, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: 'mustafalakhani'
代码如下
models.py
from django.db import models
from django.contrib.auth.models import User
from PIL import Image
class Profile(models.Model):
user=models.OneToOneField(User, on_delete=models.CASCADE, related_name='user')
image=models.ImageField(default='default.jpg',upload_to='profile_pics',blank=True)
description=models.TextField(max_length=200, blank=True)
def __str__(self):
return f'{self.user.username} Profile'
def saveimg(self):
super().save()
img=Image.open(self.image.path)
if img.height>300 or img.width>300:
output_size=(300,300)
img.thumbnail(output_size)
img.saveimg(self.image.path)
class Follower(models.Model):
follower = models.ForeignKey(User, related_name='following', on_delete=models.CASCADE)
following = models.ForeignKey(User, related_name='followers', on_delete=models.CASCADE)
class Meta:
unique_together = ('follower', 'following')
def __unicode__(self):
return u'%s follows %s' % (self.follower, self.following)