我仅使用inlineformset_factory获得最后一张表格。这是代码。我尝试了不同的答案,但找不到自己的错误。 js脚本或其他东西吗?请帮我找到它。
model.py
from django.db import models
# Create your models here.
class DAILY_REPORT(models.Model):
report_date = models.DateField() # AutoNow = True sets the time when the object created
district = models.CharField(max_length=100)
def __str__(self):
return f"{self.district}"
class GURUTTOPUNNO_NIYOMITO_MAMLA(models.Model):
daily_report = models.ForeignKey(DAILY_REPORT, on_delete=models.CASCADE)
mamlar_number = models.DecimalField(max_digits=10, decimal_places=2, null=True)
# ejaharer_description = models.TextField()
# ejaharer_ashami_number = models.DecimalField(max_digits=10, decimal_places=2)
# greptarer_number_ejahar = models.DecimalField(max_digits=10, decimal_places=2)
# greptarer_number_shondhigdho = models.DecimalField(max_digits=10, decimal_places=2)
# uddhar = models.DecimalField(max_digits=10, decimal_places=2)
# comment = models.DecimalField(max_digits=10, decimal_places=2)
class DOINIK_GREPTAR(models.Model):
daily_report = models.ForeignKey(DAILY_REPORT, on_delete=models.CASCADE)
da_b_dhara_290 = models.DecimalField(max_digits=10, decimal_places=2)
forms.py
from django import forms
from django.forms import modelformset_factory, ModelForm, inlineformset_factory
from .models import DAILY_REPORT, DOINIK_GREPTAR, GURUTTOPUNNO_NIYOMITO_MAMLA
class DAILY_REPORT_FORM(ModelForm):
district = forms.CharField(label='জেলার নাম')
#widget=forms.Textarea(attrs={"placeholder":"জেলার নাম"}))
report_date = forms.DateField()
class Meta:
model = DAILY_REPORT
fields = [
'district',
'report_date'
]
class DOINIK_GREPTAR_FORM(ModelForm):
class Meta:
model = DOINIK_GREPTAR
fields = '__all__'
class GURUTTOPUNNO_NIYOMITO_MAMLA_FORM(ModelForm):
class Meta:
model = GURUTTOPUNNO_NIYOMITO_MAMLA
exclude = ('daily_report',)
GURUTTOPUNNO_MAMLA_FORMSET = modelformset_factory(GURUTTOPUNNO_NIYOMITO_MAMLA, form=GURUTTOPUNNO_NIYOMITO_MAMLA_FORM,
exclude=('daily_report',),extra=1)
# class WeeklyProgressReportForm(forms.ModelForm):
# class Meta:
# model = WeeklyProgressReport
# fields = ('target_date', 'this_date', 'pkgno', 'slno', 'description', 'unit', 'receipt_this_week', 'issue_this_week', 'erection_this_week')
# widgets = {
# 'target_date': forms.DateInput(attrs={'class': 'form-control', 'readonly': 'readonly'}),
# 'this_date': forms.DateInput(attrs={'class': 'form-control', 'readonly': 'readonly'}),
# 'pkgno': forms.TextInput(attrs={'class': 'form-control', 'readonly': 'readonly'}),
# 'slno': forms.NumberInput(attrs={'class': 'form-control', 'readonly': 'readonly'}),
# 'description': forms.Textarea(attrs={'class': 'form-control', 'rows': 1, 'readonly': 'readonly'}),
# 'unit': forms.TextInput(attrs={'class': 'form-control', 'readonly': 'readonly'}),
# 'receipt_this_week': forms.NumberInput(attrs={'class': 'form-control', 'step': 0.01}),
# 'issue_this_week': forms.NumberInput(attrs={'class': 'form-control', 'step': 0.01}),
# 'erection_this_week': forms.NumberInput(attrs={'class': 'form-control', 'step': 0.01, 'readonly': 'readonly'}),
# }
view.py
from django.shortcuts import render
from .forms import DAILY_REPORT_FORM, DOINIK_GREPTAR_FORM, GURUTTOPUNNO_NIYOMITO_MAMLA_FORM
from django.http import HttpResponseRedirect
from django.urls import reverse
from .forms import GURUTTOPUNNO_MAMLA_FORMSET
from .models import GURUTTOPUNNO_NIYOMITO_MAMLA, DAILY_REPORT
# Create your views here.
def report_creation_view(request):
if request.method == 'GET':
base_form = DAILY_REPORT_FORM()
guruttopunno_mamla_formset = GURUTTOPUNNO_MAMLA_FORMSET(queryset=GURUTTOPUNNO_NIYOMITO_MAMLA.objects.none())
elif request.method == 'POST':
base_form = DAILY_REPORT_FORM(request.POST)
guruttopunno_mamla_formset = GURUTTOPUNNO_MAMLA_FORMSET(request.POST)
if base_form.is_valid() and guruttopunno_mamla_formset.is_valid():
print(guruttopunno_mamla_formset.cleaned_data)
base = base_form.save()
guruttopunno_mamla_formset.instance = base
guruttopunno_mamla_formset.save()
context = {
'form': base_form,
'link_formset': guruttopunno_mamla_formset,
}
return render(request, 'reports/report_creation.html', context)
def report_edit_view(request):
dl = DAILY_REPORT.objects.get(report_date='2000-02-03')
q = dl.guruttopunno_niyomito_mamla_set.all()
print(q)
base_form = DAILY_REPORT_FORM(request.POST or None, instance=dl)
guruttopunno_niyomito_mamla_formset = GURUTTOPUNNO_MAMLA_FORMSET(request.POST or None, initial=q)
if request.method == 'POST':
if base_form.is_valid() and guruttopunno_niyomito_mamla_formset.is_valid():
dl.update(**base_form.cleaned_data)
q.delete()
for guruttopunno_niyomito_mamla in guruttopunno_niyomito_mamla_formset:
GURUTTOPUNNO_NIYOMITO_MAMLA.objects.create(daily_report=dl, **guruttopunno_niyomito_mamla.cleaned_data)
context = {
'form': base_form,
'link_formset': guruttopunno_niyomito_mamla_formset,
}
return render(request, 'reports/report_creation.html', context)
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Reporting</title>
</head>
<body>
<h1>Report Submission</h1>
<form action="." method='POST'> {% csrf_token %}
<h2>Base Information</h2>
{{ form.as_p }}
<h2>Guruttopunno Mamla Shomuho</h2>
{{ link_formset.management_form }}
{% for link_form in link_formset %}
<div class="link-formset">
{{ link_form.as_p }}
</div>
{% endfor %}
<input type="submit" value="Submit" class="button"/>
</form>
<!-- Include formset plugin - including jQuery dependency -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
{% load staticfiles %}
<script src="{% static 'js/jquery.formset.js' %}"></script>
<script>
$('.link-formset').formset({
addText: 'Add',
deleteText: 'Remove'
});
</script>
</body>
</html>
我在Django文档中找不到解决方案。