I am stuck with an issue. Basically, I have to add, depending on the type of duration, a certain number of days to a date. My model and functions are like that:
models.py
from django.db import models
import datetime
# Create your models here.
FRAZIONAMENTO = (
('Annuale', 'Annuale'),
('Semestrale', 'Semestrale'),
('Quadrimestrale', 'Quadrimestrale'),
('Mensile', 'Mensile'),
)
class Polizza(models.Model):
cliente = models.ForeignKey(Cliente, on_delete=models.CASCADE)
numero_polizza = models.CharField(max_length=100)
data_decorrenza = models.DateField()
data_scadenza = models.DateField(blank=True, null=True)
ramo_polizza = models.CharField(max_length=100, choices=RAMO_POLIZZA)
polizza_nuova = models.BooleanField(default=True)
frazionamento = models.CharField(max_length=100, choices=FRAZIONAMENTO)
premio = models.DecimalField(max_digits=5, decimal_places=2)
provvigione = models.DecimalField(
max_digits=7, decimal_places=2, null=True, blank=True)
creata = models.DateTimeField(auto_now_add=True)
aggiornata = models.DateTimeField(auto_now=True)
class Meta:
verbose_name_plural = 'Polizza'
ordering = ['data_scadenza']
def __str__(self):
return self.ramo_polizza ' ' self.frazionamento
@property
def get_data_scadenza(self):
if self.frazionamento == 'Annuale':
data_scadenza = self.data_decorrenza \
str(datetime.timedelta(days=365))
elif self.frazionamento == 'Semestrale':
data_scadenza = self.data_decorrenza \
str(datetime.timedelta(days=180))
elif self.frazionamento == 'Quadrimestrale':
data_scadenza = self.data_decorrenza \
str(datetime.timedelta(days=120))
elif self.frazionamento == 'Mensile':
data_scadenza = self.data_decorrenza \
str(datetime.timedelta(days=30))
return data_scadenza
def save(self, *args, **kwargs):
self.provvigione = self.get_provvigione
super(Polizza, self).save(*args, **kwargs)
views.py
def nuova_polizza(request):
clienti = Cliente.objects.all()
polizze = Polizza.objects.all()
context = {
'clienti': clienti,
'polizze': polizze,
}
return render(request, 'anagrafica/nuova_polizza.html', context)
def salva_nuova_polizza(request):
if request.method == 'POST':
cliente = request.POST.get('cliente')
numero_polizza = request.POST.get('numero_polizza')
data_decorrenza = request.POST.get('data_decorrenza')
ramo_polizza = request.POST.get('ramo_polizza')
frazionamento = request.POST.get('frazionamento')
premio = int(request.POST.get('premio'))
polizza = Polizza(cliente_id=cliente, numero_polizza=numero_polizza, data_decorrenza=data_decorrenza,
ramo_polizza=ramo_polizza, frazionamento=frazionamento, premio=premio)
polizza.save()
return HttpResponseRedirect(reverse('anagrafica:lista_clienti'))
else:
return HttpResponse('Metodo non Consentito')
It's returning me this error:
django.core.exceptions.ValidationError: ['“2022-01-01120 days, 0:00:00” value has an invalid date format. It must be in YYYY-MM-DD format.']
I know that probably is some just tiny mistake but I couldn't find it. Any help would be highly appreciated.
CodePudding user response:
As you can see in the error message you are concattinating strings:
data_scadenza = self.data_decorrenza \
str(datetime.timedelta(days=30))
Try this:
data_scadenza = self.data_decorrenza \
datetime.timedelta(days=30)
CodePudding user response:
I found the solution in the end, which is: models.py
@property
def get_data_scadenza(self):
data_decorrenza = dt.strptime(self.data_decorrenza, '%Y-%m-%d')
if self.frazionamento == 'Annuale':
data_scadenza = data_decorrenza timedelta(days=365)
elif self.frazionamento == 'Semestrale':
data_scadenza = data_decorrenza timedelta(days=180)
elif self.frazionamento == 'Quadrimestrale':
data_scadenza = data_decorrenza timedelta(days=120)
elif self.frazionamento == 'Mensile':
data_scadenza = data_decorrenza timedelta(days=30)
return data_scadenza
Now everything works perfectly!!
