My edit view in my application won't update the court details whenever I want to edit it. I keep getting the error below. Adding, Viewing & Deleting functions are working okay. I cant find a solution on the other S/O answers. Some help on this would be appreciated. Thanks
Exception Value: 'str' object has no attribute 'court_number'
model.py
# Create your models here.
class Court(models.Model):
court_number = models.CharField(max_length=255, verbose_name='Court Number.')
accused_person = models.ForeignKey(AccusedPerson, on_delete=models.CASCADE, verbose_name='Accused Person')
court = models.CharField(choices=COURTS, max_length=255, verbose_name='Court')
court_verdict = models.CharField(choices=VERDICT, max_length=50, verbose_name='Court Status')
scheduled_on = models.DateField(verbose_name='Scheduled On')
created_by = models.ForeignKey(Profile, on_delete=models.CASCADE, verbose_name='Police Officer')
date_created = models.DateTimeField(auto_now_add=True, verbose_name='Date Created')
date_updated = models.DateTimeField(auto_now=True, verbose_name='Date Updated')
def __str__(self):
return str(self.court_number)
class Meta:
verbose_name_plural = 'Court'
forms.py
class EditCourtInfoForm(forms.Form):
court_number = forms.CharField(max_length=50, required=True, widget=forms.TextInput(attrs={'id': 'court_number', 'class': 'form-control mb-4', 'name': 'court_number', 'placeholder': 'Court Number'}))
accused_person = forms.ChoiceField(required=True, widget=forms.Select(attrs={'id': 'accused_person', 'class': 'form-control mb-4', 'name': 'accused_person', 'placeholder': 'Accused Person'}))
court = forms.ChoiceField(choices=COURTS, required=True, widget=forms.Select(attrs={'id': 'court', 'class': 'form-control mb-4', 'name': 'court', 'placeholder': 'Court'}))
court_verdict = forms.ChoiceField(choices=VERDICT, required=True, widget=forms.Select(attrs={'id': 'court_verdict', 'class': 'form-control mb-4', 'name': 'court_verdict', 'placeholder': 'Verdict'}))
scheduled_on = forms.DateField(required=True, widget=forms.DateInput(attrs={'type': 'date', 'id': 'scheduled_on', 'class': 'form-control mb-4', 'name': 'scheduled_on', 'placeholder': 'Scheduled On'}))
def __init__(self, *args, **kwargs):
super(EditCourtInfoForm, self).__init__(*args, **kwargs)
self.fields['accused_person'].choices = [(e.pk, f"{e.first_name}" + ' ' + f"{e.middle_name}" + ' ' + f"{e.last_name}") for e in AccusedPerson.objects.all()]
class Meta:
model = Court
fields = ['court_number', 'accused_person', 'court', 'court_verdict', 'scheduled_on']
views.py
def EditCourtInfo(request, id):
court = Court.objects.get(id=id)
if request.method == 'POST':
form = EditCourtInfoForm(request.POST)
if form.is_valid():
context = {'has_error': False}
court_number = form.cleaned_data['court_number']
accused_person = form.cleaned_data['accused_person']
court = form.cleaned_data['court']
court_verdict = form.cleaned_data['court_verdict']
scheduled_on = form.cleaned_data['scheduled_on']
print(scheduled_on)
court.court_number = court_number # The problem
court.accused_person = AccusedPerson.objects.get(pk=int(accused_person))
court.court = court
court.court_verdict = court_verdict
court.scheduled_on = scheduled_on
court.created_by = request.user.profile
if not context['has_error']:
court.save()
messages.success(request, '✅ Court Record Successfully Updated!')
return redirect('OfficerCourtInfo')
else:
messages.error(request, '⚠️ Court Record Was Not Updated!')
return redirect('EditCourtInfo', id=id)
else:
form = EditCourtInfoForm()
return render(request, 'Officer Edit Court.html', {'form':form, 'court':court})
The immediate problem is that you have
court = Court.objects.get(id=id)
but then later
court = form.cleaned_data['court']
So you are re-using a variable for a different purpose. You could fix this problem by using a different variable for one of these. However, you are making this much more complicated than you need to. The form will already take care of editing the Court object for you:
def EditCourtInfo(request, id):
court = Court.objects.get(id=id)
if request.method == 'POST':
form = EditCourtInfoForm(request.POST, instance=court). # pass the court object to the form
if form.is_valid():
form.save() # just save the form
messages.success(request, '✅ Court Record Successfully Updated!')
return redirect('OfficerCourtInfo')
else:
messages.error(request, '⚠️ Court Record Was Not Updated!')
return redirect('EditCourtInfo', id=id)
else:
form = EditCourtInfoForm()
return render(request, 'Officer Edit Court.html', {'form':form, 'court': court})
You will need to change your form to extend ModelForm instead of Form:
class EditCourtInfoForm(forms.ModelForm):
This code is untested, so I may have missed something. I suggest checking out the Django documentation to fill in any gaps in your understanding. You might even consider using a class-based view instead.
Related
i have custom fields in ModelForm and there is no any values on save. im just confuse what to use in view.py to save with data
form.py
class AddCityForm(forms.ModelForm):
duration = forms.ChoiceField(widget=forms.RadioSelect(attrs={
'style': 'background-color: #FAF9F9', 'class': 'mb-3, form-check-inline'}), choices=DURATION_CHOICES)
country = forms.ChoiceField(widget=forms.RadioSelect(attrs={
'style': 'background-color: #FAF9F9', 'class': 'mb-3, form-check-inline'}), choices=CITY_CHOICE)
something = forms.CharField(widget=forms.TextInput(attrs={
'style': 'background-color: #FAF9F9', 'class': 'mb-3'}))
class Meta:
model = Cities
exclude = ['city', 'duration', 'something']
view.py
def add_city(request):
data = dict()
if request.method == 'POST':
form = AddCityForm(request.POST)
if form.is_valid():
form = form.save(commit=False)
form.city = request.POST.get('country')
form.duration = request.POST.get('dur')
form.something = request.POST.get('something')
form = form.save()
messages.success(request, f'Test for Added Successfully')
data['form_is_valid'] = True
else:
data['form_is_valid'] = False
else:
form = AddCityForm()
context = dict(form=form)
data['html_form'] = render_to_string('cites/modal_1.html', context, request=request)
return JsonResponse(data)
can any one help with this ?
Looks like the code is working, i have no idea why did not before i asked this question but i will keep this if any one look for similar question
Is there a way I can get the product gotten from product = Product.objects.get(id=id) and use it in my form validation? In my template, I have listed all my products and each has the AddSaleForm. When the user fills the form and submits it sends them to the make_sale view. Now I need to check that the quantity entered from the form does not exceed the quantity each product has (as shown in my commented code in my FORMS.PY.)
Is there a way I can get the product?
MY FORMS.PY
class AddSaleForm(forms.ModelForm):
class Meta:
model = Sale
fields = ['quantity', 'selling_price']
widgets = {
'quantity': NumberInput(attrs={'class': 'form-control', 'placeholder': 'items number'}),
'selling_price': NumberInput(attrs={'class': 'form-control', 'placeholder': '1000.00'}),
}
def clean_quantity(self, *args, **kwargs):
sale_quantity = self.cleaned_data.get('quantity')
if sale_quantity == 0:
raise forms.ValidationError('The sale quantity cannot be zero (0)')
# elif sale_quantity > product_quantity: # <----rom the product accessed from make_sale view
# raise forms.ValidationError('The sale quantity cannot exceed the available quantity. '
# 'The availabe quantity for this product is ' + str(product_quantity))
return sale_quantity
MY VIEWS.PY
def make_sale(request, id):
product = Product.objects.get(id=id) # To be accessed in the form for validation
if request.method == 'POST':
form = AddSaleForm(request.POST)
if form.is_valid():
quantity = form.cleaned_data['quantity']
selling_price = form.cleaned_data['selling_price']
Sale.objects.create(product=product, quantity=quantity, selling_price=selling_price, sold_by=request.user)
product_quantity = product.quantity
sale_no = product.number_of_sales
new_sale = sale_no + 1
new_quantity = product_quantity - quantity
Product.objects.filter(id=product.id).update(quantity=new_quantity, number_of_sales=new_sale)
messages.success(request, str(quantity) + ' ' + 'item(s) sale for ' + str(product.name) + ' at Kshs. '
+ str(selling_price) + ' made successfully!')
return redirect(reverse('products:todays_sales'))
I don't know if this is a good idea to send other object to non related model form but one way is to send Product instance to your form and then use it's attributes in clean method of your AddSaleForm. Something like following code:
Goes in your views ->
product = Product.objects.get(id=id)
form = AddSaleForm(request.POST or None, initial={'product_instance': product})
And then in your clean method of AddSaleForm you have access to that instance. Following code may help you to understand what I'm saying:
Goes in your forms ->
class AddSaleForm(forms.ModelForm):
model = Sale
fields = ['quantity', 'selling_price']
widgets = {
'quantity': NumberInput(attrs={'class': 'form-control', 'placeholder': 'items number'}),
'selling_price': NumberInput(attrs={'class': 'form-control', 'placeholder': '1000.00'}),
}
def clean(self):
super().clean()
product_instance = self.initial['product_instance']
# You can add your conditions based on product instance here
I have a form, ApplyJobForm and a Formset, ApplyJobFormset. GET method works when I pass the form and the formset to a view, but for the post request the form and the formset is_valid() isn't working, after clicking submit it returns me to a view without saving. I am unable to save the form with the formset, I don't know what I'm doing wrong here.
Here are my codes.
models.py
class Applicants(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
job = models.ForeignKey(Job, on_delete=models.CASCADE, related_name='applicants')
experience = models.IntegerField(blank=True, null=True)
cv = models.FileField(upload_to=user_directory_path)
degree = models.CharField(choices=DEGREE_TYPE, blank=True, max_length=10)
created_at = models.DateTimeField(default=timezone.now)
def __str__(self):
return f'{self.user.get_full_name()} Applied'
class Certification(models.Model):
applicant = models.ForeignKey(Applicants, on_delete=models.CASCADE, related_name='applicant_certifications')
name = models.CharField(max_length=50)
certification = models.FileField(upload_to=user_directory_path, blank=True)
def __str__(self):
return f'{self.user.get_full_name()} certificate'
forms.py
class ApplyJobForm(forms.ModelForm):
class Meta:
model = Applicants
fields = ('job', 'degree', 'experience', 'cv')
exclude = ('job',)
labels = {
'degree': 'Degree',
'experience': 'Experience',
'cv': 'CV',
}
widgets = {
'degree': forms.Select(attrs={
'class': 'form-control',
}
),
'experience': forms.NumberInput(
attrs={
'class': 'form-control',
}
),
'cv': forms.FileInput(
attrs={
'class': 'form-control',
}
),
}
ApplyFormset = modelformset_factory(
Certification,
fields=('name', 'certification'),
extra=1,
widgets={
'name': forms.TextInput(
attrs={
'class': 'form-control',
'placeholder': 'Certification name'
}
),
'certification': forms.FileInput(
attrs={
'class': 'form-control',
'placeholder': 'Upload certification'
}
)
}
)
views.py
def job_apply(request, job_id=None):
template_name = 'apply_form.html'
applyform = ApplyJobForm(request.GET or None)
job = get_object_or_404(Job, id=job_id)
formset = ApplyFormset(queryset=Certification.objects.none())
if request.method == 'GET':
context = {'applyform': applyform, 'formset': formset}
return render(request, template_name, context)
elif request.method == 'POST':
applyform = ApplyJobForm(request.POST)
formset = ApplyFormset(request.POST)
if applyform.is_valid() and formset.is_valid():
apply = applyform.save(commit=False)
applyform.job = job
apply.save()
for form in formset:
# so that `apply` instance can be attached.
certification = form.save(commit=False)
certification.apply = apply
certification.save()
return redirect('successful-apply')
else:
return redirect('job-detail', id=job.id)
return render(request, template_name, {'applyform': applyform, 'formset': formset})
Here an applicant can add as many certification field when applying for a job, although the certification field is not a required field. Certification model is bound to the Applicants model.
.html
<form class="form" method="POST" action="" enctype="multipart/form-data" role="form" autocomplete="off">
.................
</form>
First of all, never redirect if your forms are not valid. You want to render your template with the invalid form so that you can display the errors to the user. This also helps debugging since you'll see the errors.
So in your view, remove these two lines:
else:
return redirect('job-detail', id=job.id)
so that the invalid case renders the forms in your template.
Next, since you have files to upload, you need to initialise forms that require files with request.FILES:
formset = ApplyFormset(request.POST, request.FILES)
(and the same for applyform).
Finally make sure that in your template you are also displaying all the errors, either on each field ({{ form.<field>.errors }}) or globally ({{ form.errors }}).
I want to save a leave in database applied from employee but when i was submit i faced this error ----LeaveApplyForm' object has no attribute 'save'---
**models.py**
class All_Leaves(models.Model):
leave_types = models.ForeignKey(Leave_Types, on_delete=models.CASCADE, related_name="all_leave")
user = models.ForeignKey(MyUser, on_delete=models.CASCADE, related_name="all_leave")
reason = models.CharField(('Reason'), max_length=120)
start_date = models.DateTimeField(('Start Date'),auto_now = True)
end_date = models.DateTimeField(('End Date'),auto_now = True)
class Meta:
verbose_name = ("All Leave")
verbose_name_plural = ("All Leaves")
**View.py**
def apply_leave(request):
print("user",request.user)
print("data",request.POST)
leave_type_data = Leave_Types.objects.all()
if request.method =="POST":
form = LeaveApplyForm(request.POST, None)
print("form status",form.is_valid())
if form.is_valid():
form.save(leave_types=request.POST['leave_types'], user=request.user)
print("form in")
return render(request, 'dashboard.html',{"var_leave":"false","leave_type_data":leave_type_data})
else:
print("form errors",form.errors)
return render(request, 'dashboard.html',{"var_leave":"false","leave_type_data":leave_type_data,"form":form})
forms.py
class LeaveApplyForm(forms.Form):
start_date = forms.DateTimeField(required=True, error_messages={'required': 'Please Choose Start Date.'})
end_date = forms.DateTimeField(required=True, error_messages={'required': 'Please Select End Date.'})
reason = forms.CharField(required=True, error_messages={'required': 'Please Enter Your Reason.'})
class Meta:
model = All_Leaves
fields = ('start_date','end_date','reason')
You've inherited from forms.Form instead of forms.ModelForm.
My view passes an id to my form. This id is a foreign key from another table. I am not able to save the id in the database table.
(id : voucher_id, table in which i am saving the form : TmpPlInvoicedet)
What i want to do
Send voucher_id from (View) to ---> TmpFormDetForm (Form) ---> TmpPlInvoicedet (DB)
Trying to get instance from the table 'TmpPlInvoice' (which has voucher_id as PK) and save it in the form gives me
DoesNotExist at /new/ TmpPlInvoice matching query does not exist
What am i doing wrong?
Views.py
def new_invoic(request):
# Create a voucher id according to my criteria
temp_vid = TmpPlInvoice.objects.order_by().values_list("voucher_id", flat=True).distinct()
if not temp_vid:
voucher_id = str(1).zfill(4)
else:
voucher_id = str(int(max(temp_vid)) + 1).zfill(4)
# POST METHOD TRying to show the voucher_id in the form in readonly format
if request.method == 'POST':
form_pk = TmpForm(request.POST or None, voucher_id=voucher_id,initial={'voucher_id': voucher_id})
if form.is_valid():
form_pk.save()
form = TmpFormDetForm(request.POST or None, voucher=voucher_id, initial={'voucher': voucher_id})
# My assumption is that since i have save the voucher_id in the TmpInvoice table so i can get the PK voucher_id value and save it in the TmpInvoiceDetForm
form.save()
return HttpResponseRedirect('/new/')
else:
return render_to_response('test.html',{'form': form, 'form_pk': form_pk},context_instance=RequestContext(request))
else:
form_pk = TmpForm(voucher_id=voucher_id,initial={'voucher_id': voucher_id})
form = TmpFormDetForm(voucher=voucher_id, initial={'voucher': voucher_id})
return render_to_response('test.html',{'form': form, 'form_pk': form_pk},context_instance=RequestContext(request))
Forms.py
# This form contains the FK. This one is giving errors while saving.
class TmpFormDetForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
voucher = kwargs.pop('voucher', None)
super(TmpFormDetForm, self).__init__(*args, **kwargs)
self.fields['voucher'].initial = TmpPlInvoice.objects.get(voucher_id=voucher)
voucher = forms.CharField(widget=forms.TextInput(attrs={'size':'40'}))
class Meta:
model = TmpPlInvoicedet
exclude = ['emp_id','particulars','qty', 'rate' , 'itemtot', 'stock_code' ]
widgets = {
'voucher': forms.TextInput(attrs={'class': 'form-control', 'placeholder': '', 'required': 'False', 'name': 'voucher','readonly': 'readonly'}),
'lineitem': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Add Total', 'required': 'False', 'blank': 'True'})}
# This form takes the PK. I save the PK here first.
class TmpForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
voucher_id = kwargs.pop('voucher_id', None)
super(TmpFor, self).__init__(*args, **kwargs)
self.fields['voucher_id'].initial = voucher_id
pos_code = MyModelChoiceField(queryset=Positions.objects.all(), widget=forms.Select(attrs={'class': 'select2_single form-control', 'blank': 'True'}))
cust = MyModelChoiceField(queryset=Custodian.objects.all(), to_field_name='acct_id',widget=forms.Select(attrs={'class': 'select2_single form-control', 'blank': 'True'}))
acct = MyModelChoiceField(queryset=Item.objects.all(), to_field_name='stock_code',widget=forms.Select(attrs={'class':'select2_single form-control', 'blank': 'True'}))
voucher_date = forms.DateField(widget=forms.TextInput(attrs={'tabindex': '-1', 'class': 'form-control has-feedback-left', 'id': 'single_cal1','aria-describedby': 'inputSuccess2Status'}))
class Meta:
model = TmpPlInvoice
exclude = ['net_amt', 'post_date', 'address', 'posted']
widgets = {
'voucher_id': forms.TextInput(attrs={'class': 'form-control', 'placeholder': '', 'required':'False', 'name': 'voucher_id', 'readonly': 'readonly'}),
'voucher_date': forms.TextInput(attrs={'tabindex': '-1', 'class': 'form-control has-feedback-left', 'id': 'single_cal1','aria-describedby': 'inputSuccess2Status'}),
'particulars': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Add Particulars', 'required':'False'}),
}
Models.py
class TmpPlInvoicedet(models.Model):
stock_code = models.CharField(max_length=13, blank=True, null=True)
voucher = models.ForeignKey(TmpPlInvoice, db_column='voucher_id')
lineitem = models.CharField(max_length=6)
particulars = models.CharField(max_length=200, blank=True, null=True)
qty = models.FloatField(blank=True, null=True)
rate = models.FloatField(blank=True, null=True)
itemtot = models.FloatField(blank=True, null=True)
emp_id = models.CharField(max_length=8, blank=True, null=True)
class Meta:
managed = False
db_table = 'tmp_pl_invoicedet'
unique_together = (('voucher', 'lineitem'),)
Easy peesy.
def master_detail(request):
def get_new_voucher_id():
temp_vid = TmpPlInvoice.objects.order_by().values_list("voucher_id", flat=True).distinct()
logger.info('Voucher ID already present %s', temp_vid)
if not temp_vid:
voucher_id = str(1).zfill(4)
else:
voucher_id = str(int(max(temp_vid)) + 1).zfill(4)
return voucher_id
voucher_id = get_new_voucher_id()
author_form = TmpForm(initial={'voucher_id': voucher_id})
author = TmpPlInvoice()
BookFormSet = inlineformset_factory(TmpPlInvoice, TmpPlInvoicedet, exclude=('emp_id', 'itemtot', 'voucher', 'lineitem','id'),
form=TmpFormDetForm, extra=1)
formset = BookFormSet(instance=author)
if request.method == 'POST':
logger.info('*'*50)
author = TmpForm(request.POST, initial={'voucher_id': voucher_id})
if author.is_valid():
logger.info('Data for Author is %s', author.cleaned_data)
created_author = author.save()
formset = BookFormSet(request.POST, instance=created_author)
if formset.is_valid():
logger.info('Data for Book is %s', formset.cleaned_data)
formset.save()
else:
logger.info('Formset errors %s', formset.errors)
else:
logger.info('Master form errors %s', author.errors)
logger.info('*'*50)
return HttpResponseRedirect('/new/')
else:
logger.info('Formset from GET is %s', formset.errors)
return render_to_response('new_invoice.html',
{'form': author_form, 'formset': formset},context_instance=RequestContext(request))
You seem to be creating a new invoice ID and then, in your form, attempting to get the invoice matching that ID. But that invoice doesn't exist yet, of course, because you haven't created it.
You might want to use get_or_create to ensure that the invoice is created if it doesn't exist.