I have a account model, which can store up to six max_values . Those max_values ranges from 0 to 500 000. Each max_value field has a correlating percentage_value field.
Im trying to write a view that finds what max_value field has a value from 0 to 50000, and what number field it has, so I cant get the correct percentage_value field.
class Account(models.Model):
name = models.CharField(max_length=500)
max_value_1 = models.BigIntegerField(max_length=20, null=True, blank=True)
max_value_2 = models.BigIntegerField(max_length=20, null=True, blank=True)
max_value_3 = models.BigIntegerField(max_length=20, null=True, blank=True)
max_value_4 = models.BigIntegerField(max_length=20, null=True, blank=True)
max_value_5 = models.BigIntegerField(max_length=20, null=True, blank=True)
max_value_6 = models.BigIntegerField(max_length=20, null=True, blank=True)
percentage_1 = models.DecimalField(max_digits=8, decimal_places=3, null=True, blank=True)
percentage_2 = models.IntegerField(max_digits=8, decimal_places=3, null=True, blank=True)
percentage_3 = models.IntegerField(max_digits=8, decimal_places=3, null=True, blank=True)
percentage_4 = models.IntegerField(max_digits=8, decimal_places=3, null=True, blank=True)
percentage_5 = models.IntegerField(max_digits=8, decimal_places=3, null=True, blank=True)
percentage_6 = models.IntegerField(max_digits=8, decimal_places=3, null=True, blank=True)
Here is my view so far:
class Account_list_50k(ListView):
context_object_name = "account_list"
#queryset should only get objects with max_value from 0 to 50000, how cant I do that?
queryset = Account.objects.all()
for Account in queryset:
def get_context_data(self, **kwargs):
context = super(Account_list_50k, self).get_context_data(**kwargs)
for num in xrange(1, 7):
if getattr(Account, 'max_value_{}'.format(num)) >= '50000':
context['percentage'] = getattr(self, 'percentage_{}'.format(num))
break
return context