View for getting field based on value from another field

88 views
Skip to first unread message

Tomas Jacobsen

unread,
Oct 18, 2012, 6:03:17 AM10/18/12
to django...@googlegroups.com
Hello!

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.

My model.py:

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


I get this error:

type object 'Account' has no attribute 'max_value_1'


Any help would be great!

Bill Freeman

unread,
Oct 18, 2012, 2:12:29 PM10/18/12
to django...@googlegroups.com
This may or may not be the cause, but spelling the variable in your
loop exactly the same as the name of your model class feels
hazzardous. Common practice would be to at least lowercase the name
to use it as a variable referring to an instance.

Bill
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/f2pi7RmIyz8J.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
Message has been deleted

Tomas Jacobsen

unread,
Oct 20, 2012, 11:52:25 AM10/20/12
to django...@googlegroups.com, wlf...@ix.netcom.com
Thank you!

kl. 20:48:27 UTC+2 torsdag 18. oktober 2012 skrev Dennis Lee Bieber følgende:
On Thu, 18 Oct 2012 03:03:17 -0700 (PDT), Tomas Jacobsen
<po...@tomasjacobsen.com> declaimed the following in
gmane.comp.python.django.user:
        <shudder>

1)        That is a very non-normalized schema.
2)        What is "percentage" based upon? Is it

        max/sum(all max)

        It sure looks like it should be a computed field, not something
stored in the database itself.

        In raw pseudo-SQL I'd look at this as

create table Account
(
        ID        integer auto-increment primary key not null,
        Name varchar(500) not null
)

create table AccountMax
(
        ID integer auto-increment primary key not null,
        AccountID integer foreign key references Account(ID),
        position integer not null,
        maxValue bigint not null
)

with selection being something like

select a.Name, am.position, am.maxValue,
                am.maxValue / sum(am.maxValue) as "percentage"
from Account as a inner join AccountMax as am
        on a.ID = am.AccountID
group by a.Name
        having am.maxValue < 50000

(remember, that is untested pseudo-code.

--
        Wulfraed                 Dennis Lee Bieber         AF6VN
        wlf...@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

Reply all
Reply to author
Forward
0 new messages