In my model, I have calculated property current_tobe_payed
I want to generate CSV report of all rows where my property current_tobe_payed is less than zero
See my view below:
def export_leaseterm_csv(request):
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="leaseterm.csv"'
writer = csv.writer(response)
leaseterms = serializers.serialize( "python", LeaseTerm.objects.all())
[obj for obj in leaseterms if obj.current_tobe_payed > 0]
for leaseterm in obj:
writer.writerow(leaseterm['fields'].values())
return response
However, I am getting an error:
'dict' object has no attribute 'current_tobe_payed'
How can I solve this issue?
(also I want to enter only certain fields into CSV and not all the table.)
UPDATE:
See my model below:
class LeaseTerm(CommonInfo):
version = IntegerVersionField( )
start_period = models.ForeignKey(Period, related_name='start_period' )
end_period = models.ForeignKey(Period, related_name='end_period')
lease = models.ForeignKey(Lease)
increase = models.DecimalField(max_digits=7, decimal_places=2)
amount = models.DecimalField(max_digits=7, decimal_places=2)
is_terminated = models.BooleanField(default=False)
# _total = None
_current_period = None
_total_current = None
_total_payment = None
_total_current_payment = None
_total_discount = None
_total_current_discount = None
_current_tobe_payed = None
_current_balance = None
def _get_total(self):
from payment.models import LeasePayment
from conditions.models import LeaseDiscount
total_payment_dict = LeasePayment.objects.filter(leaseterm_id=self.id, is_active = True ).aggregate(Sum('amount'))
if total_payment_dict ['amount__sum']:
total_payment = total_payment_dict['amount__sum']
else:
total_payment = 0
total_discount_dict = LeaseDiscount.objects.filter(leaseterm_id=self.id, is_active = True ).aggregate(Sum('amount'))
if total_discount_dict ['amount__sum']:
total_discount = total_discount_dict['amount__sum']
else:
total_discount = 0
# current = Period.objects.filter( is_active = True, _is_current = True )
current_date=datetime.datetime.now().date()
current_period_dict = Period.objects.filter(start_date__lte=current_date,end_date__gte=current_date, is_active = True ).aggregate(Max('order_value'))
#self._current_period = current_period
if current_period_dict['order_value__max']:
current_period = current_period_dict['order_value__max']
else:
current_period = 0
current_discount_dict = LeaseDiscount.objects.filter(leaseterm_id=self.id,
is_active = True, period_date__gte=self.start_period,
period_date__lte=current_period).aggregate(Sum('amount'))
if current_discount_dict ['amount__sum']:
current_discount = current_discount_dict['amount__sum']
else:
current_discount = 0
current_periods_number = current_period - self.start_period.order_value + 1
current_tobe_payed = current_periods_number * self.amount - current_discount
current_balance = total_payment - current_tobe_payed
self._current_period = current_period
self._total_payment = total_payment
self._total_discount = total_discount
self._current_tobe_payed = current_tobe_payed
self._current_balance = current_balance
@property
def current_tobe_payed(self):
if self._current_tobe_payed is None:
self._get_total()
return self._current_tobe_payed
@property
def current_balance(self):
if self._current_balance is None:
self._get_total()
return self._current_balance
@property
def current_period(self):
if self._current_period is None:
self._get_total()
return self._current_period
@property
def total_payment(self):
if self._total_payment is None:
self._get_total()
return self._total_payment
@property
def total_discount(self):
if self._total_discount is None:
self._get_total()
return self._total_discount
def clean(self):
model = self.__class__
if self.lease_id and (self.is_terminated == False) and (self.is_active == True) and model.objects.filter(lease=self.lease, is_active=True ).exclude(id=self.id).count() == 1:
raise ValidationError('!Lease has a active condition already, Terminate prior to creation of new one'.format(self.lease))
def save(self, *args, **kwargs):
self.full_clean()
return super(LeaseTerm, self).save(*args, **kwargs)
def __unicode__(self):
return u'%s %i %s %s ' % ("term:",self.id, self.start_period, self.end_period)