我有一个带有可选字段的复杂模型,我尝试使用表单保存它,每次都会收到错误消息:“ FOREIGN KEY约束失败”
我在表中的所有字段都是可选的,并且表单为空,因此应允许您保存空模型。
models.py
class Ordering(models.Model):
massage_product = models.OneToOneField(Product, on_delete=models.CASCADE, null=True, blank=True, default = 1)
masseurs = models.OneToOneField(Masseurs, on_delete=models.CASCADE, null=True, blank=True, default = 1)
massage_time_interval = models.CharField(max_length=5, blank=True)
price = models.CharField(max_length=5, blank=True)
day_week = models.CharField(max_length=15, choices=DAY_OF_WEEK, default=date.today, blank=True)
time = models.OneToOneField(Time, on_delete=models.CASCADE, null=True, blank=True, default = 1)
place = models.OneToOneField(PlaceMassage, on_delete=models.CASCADE, null=True, blank=True, default = 1)
payment_method = models.CharField(max_length=1, choices=PAYMENT_METHODS, default='G', blank=True)
name = models.CharField(max_length=50, blank=True)
surname = models.CharField(max_length=50, blank=True)
e_mail = models.EmailField(blank=True)
additional_requests = models.TextField(blank=True)
phone_number = models.CharField(max_length=15, blank=True)
reminder_visit = models.BooleanField(default=True, blank=True)
website_rules = models.BooleanField(default=True, blank=True)
city = models.CharField(max_length=50, blank=True)
street = models.CharField(max_length=50, blank=True)
street_number = models.CharField(max_length=10, blank=True)
hom_number = models.CharField(max_length=10, blank=True)
zip_code = models.CharField(max_length=10, blank=True)
created_at_data = models.DateTimeField(auto_now_add=True)
updated_at_data = models.DateTimeField(auto_now=True)
forms.py
if request.method == 'POST' and 'btn_massage_order' in request.POST:
ordering_form = OrderingMassageForm(data=request.POST)
if ordering_form.is_valid():
ordering = OrderingMassage()
ordering.save()
else:
ordering_form = OrderingMassageForm()
错误 环境:
Request Method: POST
Request URL: http://127.0.0.1:8000/massage_order/2_1_90_100/
Django Version: 2.0
Python Version: 3.7.0
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app',
'multiselectfield',
'crispy_forms']
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:
File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\base.py" in _commit
239. return self.connection.commit()
The above exception (FOREIGN KEY constraint failed) was the direct cause of the following exception:
File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\exception.py" in inner
35. response = get_response(request)
File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\base.py" in _get_response
128. response = self.process_exception_by_middleware(e, request)
File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\base.py" in _get_response
126. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\tymot\Desktop\PROJEKT\app_test_form-czysty\app_rama\app\views.py" in massage_order
399. ordering.save()
File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\base.py" in save
729. force_update=force_update, update_fields=update_fields)
File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\base.py" in save_base
759. updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\transaction.py" in __exit__
212. connection.commit()
File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\base.py" in commit
261. self._commit()
File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\base.py" in _commit
239. return self.connection.commit()
File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\utils.py" in __exit__
89. raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\tymot\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\base.py" in _commit
239. return self.connection.commit()
Exception Type: IntegrityError at /massage_order/2_1_90_100/
Exception Value: FOREIGN KEY constraint failed
Django admin允许您保存一个空表格,为什么我不允许呢?任何帮助将不胜感激。
答案 0 :(得分:1)
尝试
if request.method == 'POST' and 'btn_massage_order' in request.POST:
ordering_form = OrderingMassageForm(request.POST)
if ordering_form.is_valid():
ordering = ordering_form(commit=False)
ordering.save()
else:
ordering_form = ordering_form()
希望有帮助
答案 1 :(得分:1)
您的模型中具有以下一对一关系:
massage_product = models.OneToOneField(Product, ... default = 1)
masseurs = models.OneToOneField(Masseurs, ... default = 1)
这意味着每个Ordering
和每个Order
({{1只能有一个Product
(您是说Masseurs
? }}?)。这可能不是您想要的。除此之外,您还可以设置默认值Masseur
,该值可以运行一次,但不会运行两次,因为ID为1
的{{1}} / Product
仅可以与一个记录在Masseurs
中。
我怀疑这是您失败的FK约束的来源。
这是Django docs对1
的评价:
从概念上讲,这类似于具有unique = True的ForeignKey