【问题标题】:How to submit order form and get redirect to the user's page to see all others made by the user如何提交订单并重定向到用户页面以查看用户制作的所有其他表单
【发布时间】: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


【解决方案1】:

null value in column "location_id" of relation "clients_clientjob" violates not-null constraint

此错误告诉我您收到此错误是因为您没有为表单提供位置属性。首先,让我告诉您解决此错误的方法。

1.) location = models.ForeignKey('accounts.Area' ,on_delete =models.CASCADE,null=True,blank=True) 使位置字段可以为空和空白。

2.) 为位置提供价值。

现在为什么实际发生此错误。这可能是因为您在提交为 false 的情况下保存表单,但随后您保存的表单中没有位置字段。

            #customer = ClientJob.objects.create(customer=product.customer)
       
            #product.customer =customer
           
            product.save()```
But I also don't understand why the form is being validated in the first place but I think this should fix your problem.

3.) You can provide location in the view itself.

【讨论】:

  • 我如何在视图中提供位置?@Dakshesh Jain
  • 您正在使用 ForeignKey,因此您必须使用类似 location_qs = location_model.objects.get(id=1) product.save(location=location_qs) 的方式提供位置。试试这个
  • 客户 选择一个有效的选项。该选择不是可用的选择之一。以上是现在显示的错误。我该如何解决?
猜你喜欢
  • 1970-01-01
  • 2018-07-13
  • 2014-04-13
  • 2012-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-18
  • 2013-07-18
相关资源
最近更新 更多