У меня есть одно приложение с именем prets, я создаю новый кредит, затем распечатываю таблицу emprunteers, когда у каждого emprunteur есть таблица месяцев для оплаты своего кредита, поэтому я хочу опубликовать таблицу каждого emprunteur
models.py
class Garantie(models.Model):
type = models.CharField(max_length=10)
def __str__(self):
return self.type
class NewCredit(models.Model):
""" Garantie = (
(1, 'Chéque'),
(2, 'Immobilier'),
(3, 'Terrain'),
)"""
Matricule_emprunteur = models.CharField(max_length=30,null=True)
Nom_prénom = models.CharField(max_length=100)
Num_compte = models.CharField(max_length=20,null=True)
Début_paiement = models.DateField('Date_début_paiement (MM/dd/yyyy)', null=True)
Fin_paiement = models.DateField('Date_fin_paiement (MM/dd/yyyy)', null=True)
Libellé_garantie = models.ForeignKey(Garantie, on_delete=models.CASCADE)
Valeur_garantie = models.CharField(max_length=30,null=True)
Montant = models.CharField(max_length=30,null=True)
Taux_interet = models.CharField(max_length=30,null=True)
Interet_rem = models.CharField(max_length=30,null=True)
Total_rem = models.CharField(max_length=30,null=True)
def __str__(self):
return self.Matricule_emprunteur
class PaieCredit(models.Model):
emprunteur= models.OneToOneField(NewCredit, on_delete=models.CASCADE, primary_key=True,default='0')
Mois = models.CharField(max_length=50)
Archiver = models.BooleanField(default=False)
def __str__(self):
return self.Matricule_emprunteur
forms.py
class CreditForm(forms.ModelForm):
class Meta:
model = NewCredit
fields = '__all__'
def __init__(self, *args, **kwargs):
super(CreditForm,self).__init__(*args, **kwargs)
self.fields['Libellé_garantie'].empty_label = "Select"
class PaieForm(forms.ModelForm):
class Meta:
model = PaieCredit
fields = '__all__'
views.py
def Emprunteurs_detail(request, id=0, emprunteur_id=0):
if request.method == "GET":
if id == 0:
form = CreditForm()
frm = PaieForm()
else:
emprunteur = NewCredit.objects.get(pk=id)
form = CreditForm(instance=emprunteur)
result = []
debut = emprunteur.Début_paiement
fin = emprunteur.Fin_paiement
while debut <= fin:
result.append(debut)
debut += relativedelta(months=1)
list_month = []
for x in result:
Months = x.strftime("%B") +" "+ str(x.year)
list_month.append(Months)
months = list_month
#model = PaieCredit.Mois
#print (months)
paie = PaieCredit.objects.filter(emprunteur_id=id).first()
#paie = get_object_or_404(PaieCredit, pk=emprunteur_id)
frm = PaieForm(instance=paie)
#for month in months:
model = frm.Mois
model.append(months)
model.save()
return render(request, "prets/emprunteur_detail.html", {'form': form, 'fm':frm})
else:
frm = PaieForm(request.POST or None, request.FILES or None)
#if form.is_valid():
frm.save()
return HttpResponse("update Successfully saved")
html / emprunteur_detail
{% extends "prets/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
<header>
<div>
<h4>Matricule : {{form.Matricule_emprunteur}}</h4>
<h4>Nom_prénom : {{form.Nom_prénom}}</h4>
<h4>Num_compte : {{form.Num_compte}}</h4>
<h4>Montant à remboursé : {{form.Total_rem}}</h4>
</div>
</header>
<body>
<form action="" method="POST" novalidate>
<button type="submit" class="btn btn-success btn-block btn-lg"><i class="fas fa-database"></i>
Save</button>
<table class="table table-borderless">
<thead class="border-bottom font-weight-bold">
<tr>
<td>Mois</td>
<td>Archiver</td>
</tr>
</thead>
<tbody>
<tr>
<td>{{frm.Mois}}</td>
<td>{{frm.Archiver}}</td>
</tr>
</tbody>
</form>
</body>
{% endblock content %}
Я хочу сгенерировать месяцы и сохраните его в базе данных для каждого emprunteur, я использую французские имена, так что кто-нибудь может мне помочь, пожалуйста ??