我正在尝试通过POST获取用户名,并过滤出用户并将其保存到模型中。每当我尝试迁移时,都会收到错误消息“字段'id'需要一个数字,但得到了'用户'”
def newrequest(requests):
if (requests.user.is_authenticated and requests.user.userrank.userstatus == 'Agent'):
if requests.method == 'POST':
principalamount = requests.POST['principalamount']
interest = requests.POST['interest']
emi = requests.POST['emi']
tenure = requests.POST['tenure']
applicantsalary = requests.POST['applicantsalary']
applicantname = requests.POST['applicantname']
applicantcity = requests.POST['applicantcity']
bankname = requests.POST['bankname']
accountnumber = requests.POST['accountnumber']
age = requests.POST['age']
email = requests.POST['email']
user = User.objects.get(email__email=email)
loan = Loan(
user = user,
principalamount = principalamount,
interest = interest,
emi = emi,
tenure = tenure,
applicantsalary = applicantsalary,
applicantname = applicantname,
applicantcity = applicantcity,
bankname = bankname,
accountnumber = accountnumber,
age = age,
)
loan.save()
return JsonResponse({"message": "Loan Requested"})
else:
return HttpResponseForbidden()
这是我的模特:
class Loan(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
loanid = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)
principalamount = models.IntegerField()
interest = models.IntegerField()
emi = models.IntegerField()
tenure = models.IntegerField()
applicantsalary = models.IntegerField()
applicantname = models.CharField(max_length=300)
applicantcity = models.CharField(max_length=25)
bankname = models.CharField(max_length=100)
accountnumber = models.CharField(max_length=25)
age = models.IntegerField(null=True)
这是上面相关模型文件中的Traceback错误:
Operations to perform:
Apply all migrations: admin, auth, contenttypes, loansystem, sessions
Running migrations:
Applying loansystem.0007_loan_user...Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\fields\__init__.py", line 1774, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: 'Ankur'
Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\management\__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\management\base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\management\base.py", line 371, in execute
output = self.handle(*args, **options)
File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\management\base.py", line 85, in wrapped
res = handle_func(*args, **kwargs)
File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\management\commands\migrate.py", line 243, in handle
post_migrate_state = executor.migrate(
File "C:\Users\user\AppData\Local\Programs\Python\Python38\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\user\AppData\Local\Programs\Python\Python38\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\user\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\migrations\executor.py", line 227, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\Users\user\AppData\Local\Programs\Python\Python38\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\user\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\migrations\operations\fields.py", line 104, in database_forwards
schema_editor.add_field(
File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\backends\sqlite3\schema.py", line 328, in add_field
self._remake_table(model, create_field=field)
File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\backends\sqlite3\schema.py", line 189, in _remake_table
self.effective_default(create_field)
File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\backends\base\schema.py", line 303, in effective_default
return field.get_db_prep_save(self._effective_default(field), self.connection)
File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\fields\related.py", line 971, in get_db_prep_save
return self.target_field.get_db_prep_save(value, connection=connection)
File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\fields\__init__.py", line 823, in get_db_prep_save
return self.get_db_prep_value(value, connection=connection, prepared=False)
File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\fields\__init__.py", line 2388, in get_db_prep_value
value = self.get_prep_value(value)
File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\fields\__init__.py", line 1776, in get_prep_value
raise e.__class__(
ValueError: Field 'id' expected a number but got 'Ankur'.
这是迁移文件:
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('loansystem', '0014_auto_20201007_2357'),
]
operations = [
migrations.AlterField(
model_name='loan',
name='user',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
),
]
答案 0 :(得分:1)
您正在使用user
关键字,在这种情况下,该关键字引用了 User
对象。您没有传递用户对象,而是传递了一个字典数组,类似于:[{'id': 1}, ...]
要解决此问题,请删除.values
子句:
user = User.objects.get(email__email=email)
loan = Loan(
user=user,
...
)
请注意,当用户的User.objets.get
字段不唯一时,email
将引发错误。
答案 1 :(得分:1)
userid
不是用户的ID,它是一个QuerySet
,用于包装用户ID的集合。这样做也没有多大意义,因为您可以使用以下命令立即检索用户:
from django.contrib.auth.decorators import login_required
@login_required
def newrequest(requests):
if requests.user.userrank.userstatus == 'Agent':
if requests.method == 'POST':
principalamount = requests.POST['principalamount']
interest = requests.POST['interest']
emi = requests.POST['emi']
tenure = requests.POST['tenure']
applicantsalary = requests.POST['applicantsalary']
applicantname = requests.POST['applicantname']
applicantcity = requests.POST['applicantcity']
bankname = requests.POST['bankname']
accountnumber = requests.POST['accountnumber']
age = requests.POST['age']
email = requests.POST['email']
user = User.objects.get(email__email=email)
loan = Loan(
user =user,
principalamount = principalamount,
interest = interest,
emi = emi,
tenure = tenure,
applicantsalary = applicantsalary,
applicantname = applicantname,
applicantcity = applicantcity,
bankname = bankname,
accountnumber = accountnumber,
age = age,
)
loan.save()
return JsonResponse({"message": "Loan Requested"})
else:
return HttpResponseForbidden()
话虽如此,您可能想使用ModelForm
[Django-doc]。这样可以更方便地验证和保存对象。
注意:您可以将视图限制为使用
@login_required
decorator [Django-doc]。