【发布时间】:2021-09-23 10:47:29
【问题描述】:
我正在开发一个应用程序,客户可以在其中下订单并被重定向到一个页面以查看所有请求的订单,但我在这里遇到错误。下面是错误信息。我想要的只是让注册下订单并被重定向到将显示他的所有订单的页面的用户。 如果有人可以在这里帮助我,我将不胜感激。
这是错误信息:
IntegrityError at /clients/add_item/
null value in column "location_id" of relation "clients_clientjob" violates not-null constraint
DETAIL: Failing row contains (2, , null, , , , 2021-07-14 12:10:05.555719+00, Pending, 2, null).
Request Method: POST
Request URL: https://canwork.herokuapp.com/clients/add_item/
Django Version: 3.1
Exception Type: IntegrityError
Exception Value:
null value in column "location_id" of relation "clients_clientjob" violates not-null constraint
DETAIL: Failing row contains (2, , null, , , , 2021-07-14 12:10:05.555719+00, Pending, 2, null).
Exception Location: /app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py, line 84, in _execute
Python Executable: /app/.heroku/python/bin/python
Python Version: 3.6.13
Python Path:
['/app/.heroku/python/bin',
'/app',
'/app/.heroku/python/lib/python36.zip',
'/app/.heroku/python/lib/python3.6',
'/app/.heroku/python/lib/python3.6/lib-dynload',
'/app/.heroku/python/lib/python3.6/site-packages']
Server time: Wed, 14 Jul 2021 12:10:05 +0000
这是客户端(应用程序名称)模型。此模型适用于用户想要请求的客户端作业
class ClientJob(models.Model):
STATUS = (
('Pending', 'Pending'),
#('On-going', 'On-going'),
('Completed', 'Completed'),
)
customer = models.ForeignKey('accounts.Customer', null=True,blank=True, on_delete= models.SET_NULL,related_name='client')
job_name = models.CharField(max_length=50,unique =False)
text_description = models.CharField(max_length=150,null=True)
location = models.ForeignKey('accounts.Area' ,on_delete =models.CASCADE)
address = models.CharField(max_length=200,unique =False)
phone =models.CharField(max_length=15,unique =False)
email = models.EmailField(unique = False)
date_created = models.DateTimeField(auto_now_add=True, null=True)
status = models.CharField(max_length=200, null=True, choices=STATUS,default='Pending')
def __str__(self):
return self.job_name
class Meta:
verbose_name_plural = "Client Job"
这是客户网址:
from django.urls import path
from .views import *
from .import views
app_name = 'clients'
urlpatterns = [
path('add_item/', views.insert_ClientJob ,name='add_item'),
]
这是客户端视图:
@login_required
def insert_ClientJob(request):
if request.method == 'POST':
form = ClientJobForms(request.POST)
user_id=request.POST.get('id')
customer = ClientJob.objects.create(customer=request.user.details)
if form.is_valid():
product = form.save(commit=False)
#customer = ClientJob.objects.create(customer=product.customer)
#product.customer =customer
product.save()
messages.success(request ,"successful")
return redirect('profession:user',user_id)
else:
form = ClientJobForms()
return render(request ,'job_request.html' ,{'form': form})
this is the client form:
class ClientJobForms(ModelForm):
class Meta:
model = ClientJob
fields = ['job_name','text_description','location','address','phone','email','status','customer']
#fields ="__all__"
def __init__(self, *args, **kwargs):
super(ClientJobForms, self).__init__(*args, **kwargs)
self.fields['location'].empty_label ='Your location'
this is the client admin:
from django.contrib import admin
from .models import *
# Register your models here.
class ClientJobAdmin(admin.ModelAdmin):
list_display = ('id','job_name','text_description','location','address','phone','email','date_created','status','customer')
admin.site.register(ClientJob ,ClientJobAdmin)
this is accounts model from where user model is derived from:
Account model is a different app from where i got user model
class Area(models.Model):
area_code = models.CharField(max_length=7)
location = models.CharField(max_length=100)
def __str__(self):
return self.location
class Meta:
verbose_name_plural = "Area"
class Customer(models.Model):
user = models.OneToOneField(User,null=True,blank=True, on_delete= models.SET_NULL,related_name='details')
address = models.CharField(max_length=200, null=True)
phone = models.CharField(max_length=15, null=True)
date_created = models.DateTi`enter code here`meField(auto_now_add=True, null=True)
def __str__(self):
return str(self.user)
【问题讨论】:
-
对我来说似乎很清楚:您没有发送 location_id 值
标签: python django database postgresql heroku