Я пытаюсь передать первичный ключ сохраненного экземпляра ModelForm в качестве внешнего ключа новому экземпляру запроса в Django.
Я не совсем уверен, почему это не работает. Я попытался прервать получение идентификатора внешнего ключа, и он возвращает число, но оно все еще не работает.
Любое понимание того, что я делаю неправильно, будет высоко оценено.
Просмотров .py
def customer_inquiry(request):
submitted = False
if request.method == 'POST':
form = CustomerForm(request.POST)
if form.is_valid():
customer = form.save()
identity = customer.pk
obj = Customer.objects.get(cust_id=identity)
inquiry = Inquiry.objects.create(
cust_id = Customer.objects.get(cust_id = obj.pk),
inquiry_date = datetime.now)
return HttpResponseRedirect('/customer_inquiry/?submitted=True')
else:
form = CustomerForm()
if 'submitted' in request.GET:
submitted = True
return render(request, 'customer_inquiry.html', {'form': form, 'submitted': submitted})
Model.py
inquiry_id = models.AutoField(primary_key=True)
cust_id = models.ForeignKey(Customer, on_delete=models.CASCADE)
inquiry_date = models.DateField()
class Inquiry(models.Model):
inquiry_id = models.AutoField(primary_key=True)
cust_id = models.ForeignKey(Customer, on_delete=models.CASCADE)
inquiry_date = models.DateField()
Ошибка
Request Method: POST
Request URL: http://127.0.0.1:8000/customer_inquiry/
Django Version: 3.0.3
Python Version: 3.8.1
Installed Applications:
['database',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework']
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 (most recent call last):
File "/Users//.local/share/virtualenvs/-xHaG2iwn/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/Users//.local/share/virtualenvs/-xHaG2iwn/lib/python3.8/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users//.local/share/virtualenvs/-xHaG2iwn/lib/python3.8/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users//Code/copy//bikelockerapp/bikelockerapp/views.py", line 17, in customer_inquiry
inquiry = Inquiry.objects.create(
File "/Users//.local/share/virtualenvs/-xHaG2iwn/lib/python3.8/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users//.local/share/virtualenvs/-xHaG2iwn/lib/python3.8/site-packages/django/db/models/query.py", line 433, in create
obj.save(force_insert=True, using=self.db)
File "/Users//.local/share/virtualenvs/-xHaG2iwn/lib/python3.8/site-packages/django/db/models/base.py", line 745, in save
self.save_base(using=using, force_insert=force_insert,
File "/Users//.local/share/virtualenvs/-xHaG2iwn/lib/python3.8/site-packages/django/db/models/base.py", line 782, in save_base
updated = self._save_table(
File "/Users//.local/share/virtualenvs/-xHaG2iwn/lib/python3.8/site-packages/django/db/models/base.py", line 887, in _save_table
results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
File "/Users//.local/share/virtualenvs/-xHaG2iwn/lib/python3.8/site-packages/django/db/models/base.py", line 924, in _do_insert
return manager._insert(
File "/Users//.local/share/virtualenvs/-xHaG2iwn/lib/python3.8/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/alessandrolou/.local/share/virtualenvs/CascadeBicycleClubCapstone-xHaG2iwn/lib/python3.8/site-packages/django/db/models/query.py", line 1204, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "/Users//.local/share/virtualenvs/-xHaG2iwn/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1383, in execute_sql
for sql, params in self.as_sql():
File "/Users//.local/share/virtualenvs/-xHaG2iwn/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1326, in as_sql
value_rows = [
File "/Users//.local/share/virtualenvs/-xHaG2iwn/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1327, in <listcomp>
[self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
File "/Users//.local/share/virtualenvs/-xHaG2iwn/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1327, in <listcomp>
[self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
File "/Users/alessandrolou/.local/share/virtualenvs/-xHaG2iwn/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1268, in prepare_value
value = field.get_db_prep_save(value, connection=self.connection)
File "/Users//.local/share/virtualenvs/-xHaG2iwn/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 821, in get_db_prep_save
return self.get_db_prep_value(value, connection=connection, prepared=False)
File "/Users//.local/share/virtualenvs/-xHaG2iwn/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 1220, in get_db_prep_value
value = self.get_prep_value(value)
File "/Users//.local/share/virtualenvs/-xHaG2iwn/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 1215, in get_prep_value
return self.to_python(value)
File "/Users//.local/share/virtualenvs/-xHaG2iwn/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 1177, in to_python
parsed = parse_date(value)
File "/Users//.local/share/virtualenvs/-xHaG2iwn/lib/python3.8/site-packages/django/utils/dateparse.py", line 75, in parse_date
match = date_re.match(value)
Exception Type: TypeError at /customer_inquiry/
Exception Value: expected string or bytes-like object```