Re: View for getting field based on value from another field
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:
> 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)
>
<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/
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/-/UYq7PLTUyEkJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home