您好,我正在嘗試自動將客戶保存在郵寄中,而無需將其列在表格中。它目前顯示下拉選單并正確保存,但如果我從 forms.py 中洗掉客戶,它就不會再保存了。視圖.py
@login_required(login_url='login')
def createInfringer(request):
customer=request.user.customer
form = InfringerForm(customer=customer)
if request.method == 'POST':
form = InfringerForm(customer, request.POST)
if form.is_valid():
form.save()
return redirect('infringer-list')
context ={'form': form}
return render (request, 'base/infringement_form.html', context)
表單.py
class InfringerForm(ModelForm):
def __init__(self, customer, *args, **kwargs):
super(InfringerForm,self).__init__(*args, **kwargs)
self.fields['customer'].queryset = Customer.objects.filter(name=customer)
self.fields['status'].queryset = Status.objects.filter(customer=customer)
class Meta:
model = Infringer
fields = ['name', 'brand_name','status','customer']
添加了下面的更新建議,但它仍然無法挽救客戶。
uj5u.com熱心網友回復:
如果我正確理解您的問題,您希望將客戶保存在您的模型中,但不希望在您的表單上顯示客戶欄位,因為客戶是登錄用戶。如果該假設是正確的,您需要首先從表單欄位及其__init__方法中洗掉客戶欄位。然后,您需要在發布請求期間將客戶傳遞給您的保存方法,因為您的模型可能需要該欄位:
@login_required(login_url='login')
def createInfringer(request):
customer=request.user.customer
form = InfringerForm(customer=customer)
if request.method == 'POST':
form = InfringerForm(customer, request.POST)
if form.is_valid():
saved_instance = form.save(customer)
print (f'Successfully saved the infringer with its customer {saved_instance.customer}') ## Insert this and see what it says
return redirect('infringer-list')
context ={'form': form}
return render (request, 'base/infringement_form.html', context)
class InfringerForm(ModelForm):
class Meta:
model = Infringer
# fields = ['name', 'brand_name','status','customer']
fields = ['name', 'brand_name','status'] # Notice the above commented line. Also, Add this instead
def __init__(self, customer, *args, **kwargs):
super(InfringerForm,self).__init__(*args, **kwargs)
# self.fields['customer'].queryset = Customer.objects.filter(name=customer)
self.fields['status'].queryset = Status.objects.filter(customer=customer)
def save(self, customer, *args, **kwargs):
instance = super(InfringerForm, self).save( *args, **kwargs)
if customer:
print (f'customer is {customer}')
self.customer = customer
instance.save()
print (f'instance was saved with the customer {instance.customer}')
return instance
我沒有測驗上面的代碼,但它應該可以作業
uj5u.com熱心網友回復:
表單.py
class InfringerForm(ModelForm): class Meta: model = Infringer fields = ['name', 'brand_name', 'status'] def __init__(self, customer, *args, **kwargs): super().__init__(*args, **kwargs) self.customer = customer self.fields['status'].queryset = Status.objects.filter(customer=customer) def save(self, *args, **kwargs): self.instance.customer = self.customer return super().save( *args, **kwargs)
視圖.py
@login_required(login_url='login') def createInfringer(request): customer = request.user.customer form = InfringerForm(customer=customer) if request.method == 'POST': form = InfringerForm(customer, request.POST, request.FILES) if form.is_valid(): saved_instance = form.save() print (f'customer in views.py is {customer}') print (f'Successfully saved the infringer in views.py with its customer {saved_instance.customer}') return redirect('infringer-list') return render (request, 'base/infringement_form.html', {'form': form})
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/537050.html
標籤:Python姜戈形式
