Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » percentage of models given field value? [RSS Feed]

#1 Nov. 25, 2010 00:37:55

Lachlan M.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

percentage of models given field value?


Hola,

Using django 1.2 I wanted to present some statistics on the data that
we have been collecting in our db.

We have source texts with authors, and target texts with their translators.

The target text model has a value "Language".

I get the count of the target texts: all, and then filtered by
language. How would I present each language as a percentage of the
total target text count?

cheers
L.


views.py

def statistics(request):
"""
collects counts on all models: Source, Target, Authors, Translators
and languages for basic stats page view
"""
'''
target text stats
'''
target_count = TargetText.objects.all().count()
target_japanese = TargetText.objects.filter(language="ja").count()
target_spanish = TargetText.objects.filter(language="es").count()
target_italian = TargetText.objects.filter(language="it").count()
target_tradchinese = TargetText.objects.filter(language="zh-cn").count()
target_simpchinese = TargetText.objects.filter(language="zh-tw").count()
'''
translator stats
'''
translator_count = Translator.objects.all().count()
translator_spanish = Translator.objects.filter(language="es").count()
trans_span_percent = translator_spanish / translator_count
translator_japanese = Translator.objects.filter(language="ja").count()
translator_italian = Translator.objects.filter(language="it").count()
translator_simpchinese = Translator.objects.filter(language="zh-tw").count()
translator_tradchinese = Translator.objects.filter(language="zh-cn").count()

source_count = SourceText.objects.all().count()
author_count = Author.objects.all().count()
lang_dict = dict(LANGUAGES)
langs = TargetText.objects.values_list('language',
flat=True).distinct().order_by('language')
long_langs = [lang_dict for lang in langs]
return render_to_response('library/stats.html', locals())

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Nov. 25, 2010 02:44:38

Christophe P.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

percentage of models given field value?


On Nov 24, 2010, at 4:37 PM, Lachlan Musicman wrote:
> Using django 1.2 I wanted to present some statistics on the data that
> we have been collecting in our db.
>
> We have source texts with authors, and target texts with their translators.
>
> The target text model has a value "Language".
>
> I get the count of the target texts: all, and then filtered by
> language. How would I present each language as a percentage of the
> total target text count?

This is a great query to write as SQL, rather than using the ORM (I love the
Django ORM, but the right tools for the right job).

Assuming more or less standard Django names, you could return a list of the
languages, the total number of texts for each language, and the percentage of
the total with something along the lines of:

SELECT language, COUNT(*), (COUNT(*)/(SELECT COUNT(*) FROM
app_targettexts))
FROM app_targettexts
GROUP BY language

This has the advantage that it only hits the database once. If you need the
grand total, you can either calculate it in the application, or thorw on:

UNION
SELECT 'total', COUNT(*), 1.0 FROM app_targettexts

--
-- Christophe Pettus
x...@thebuild.com

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 Nov. 25, 2010 04:14:10

Lachlan M.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

percentage of models given field value?


On Thu, Nov 25, 2010 at 13:44, Christophe Pettus <x...@thebuild.com> wrote:
>
> On Nov 24, 2010, at 4:37 PM, Lachlan Musicman wrote:
>> Using django 1.2 I wanted to present some statistics on the data that
>> we have been collecting in our db.
>>
>> We have source texts with authors, and target texts with their translators.
>>
>> The target text model has a value "Language".
>>
>> I get the count of the target texts: all, and then filtered by
>> language. How would I present each language as a percentage of the
>> total target text count?
>
> This is a great query to write as SQL, rather than using the ORM (I love the
> Django ORM, but the right tools for the right job).
>
> Assuming more or less standard Django names, you could return a list of the
> languages, the total number of texts for each language, and the percentage of
> the total with something along the lines of:
>
>        SELECT language, COUNT(*), (COUNT(*)/(SELECT COUNT(*) FROM
> app_targettexts))
>                FROM app_targettexts
>                GROUP BY language
>
> This has the advantage that it only hits the database once.  If you need the
> grand total, you can either calculate it in the application, or thorw on:
>
>        UNION
>        SELECT 'total', COUNT(*), 1.0 FROM app_targettexts

Thanks Christopher. I've never done raw SQL in Django before. This may
seem like a silly follow up question, but is it standard practice to
put the relevant code, as described by the documentation
(http://docs.djangoproject.com/en/dev/topics/db/sql/#executing-custom-sql-directly) into views.py?

cheers
L.

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#4 Nov. 25, 2010 04:54:34

Christophe P.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

percentage of models given field value?


On Nov 24, 2010, at 8:13 PM, Lachlan Musicman wrote:
> Thanks Christopher. I've never done raw SQL in Django before. This may
> seem like a silly follow up question, but is it standard practice to
> put the relevant code, as described by the documentation
> (http://docs.djangoproject.com/en/dev/topics/db/sql/#executing-custom-sql-directly> ) into views.py?

It's largely a matter of taste. I prefer putting raw SQL in the Model subclass
(as a static or class method), and have the results come back in a form that is
usable for the view. There's no reason that the SQL can't be in the view
function, but it's cleaner to have the Model encapsulate all of the related
data access for a particular model; that way, if the fields in the model
subclass change, there's only one place to look.

--
-- Christophe Pettus
x...@thebuild.com

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#5 Nov. 25, 2010 05:00:52

Christophe P.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

percentage of models given field value?


On Nov 24, 2010, at 8:13 PM, Lachlan Musicman wrote:
> Thanks Christopher. I've never done raw SQL in Django before. This may
> seem like a silly follow up question, but is it standard practice to
> put the relevant code, as described by the documentation
> (http://docs.djangoproject.com/en/dev/topics/db/sql/#executing-custom-sql-directly> ) into views.py?

Oh, and: Another very logical place to put this is in a custom Manager subclass
for the relevant Model; there's documentation on that usage in the docs:http://docs.djangoproject.com/en/1.2/topics/db/managers/#adding-extra-manager-methods--
-- Christophe Pettus
x...@thebuild.com

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#6 Nov. 25, 2010 15:37:44

Eric T.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

percentage of models given field value?


Why not make a function in the model return those results?
On 11/24/10 10:13 PM, Lachlan Musicman wrote:On Thu, Nov 25, 2010 at 13:44, Christophe Pettus<x...@thebuild.com> wrote:On Nov 24, 2010, at 4:37 PM, Lachlan Musicman wrote:Using django 1.2 I wanted to present some statistics on the data that
we have been collecting in our db.

We have source texts with authors, and target texts with their translators.

The target text model has a value "Language".

I get the count of the target texts: all, and then filtered by
language. How would I present each language as a percentage of the
total target text count?This is a great query to write as SQL, rather than using the ORM (I love the
Django ORM, but the right tools for the right job).

Assuming more or less standard Django names, you could return a list of the
languages, the total number of texts for each language, and the percentage of
the total with something along the lines of:

SELECT language, COUNT(*), (COUNT(*)/(SELECT COUNT(*) FROM
app_targettexts))
FROM app_targettexts
GROUP BY language

This has the advantage that it only hits the database once. If you need the
grand total, you can either calculate it in the application, or thorw on:

UNION
SELECT 'total', COUNT(*), 1.0 FROM app_targettextsThanks Christopher. I've never done raw SQL in Django before. This may
seem like a silly follow up question, but is it standard practice to
put the relevant code, as described by the documentation
(http://docs.djangoproject.com/en/dev/topics/db/sql/#executing-custom-sql-directly) into views.py?

cheers
L.--
You received this message because you are subscribed to the Google Groups "Django
users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#7 Dec. 9, 2010 03:13:52

Lachlan M.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

percentage of models given field value?


On Thu, Nov 25, 2010 at 16:00, Christophe Pettus <x...@thebuild.com> wrote:
>
> On Nov 24, 2010, at 8:13 PM, Lachlan Musicman wrote:
>> Thanks Christopher. I've never done raw SQL in Django before. This may
>> seem like a silly follow up question, but is it standard practice to
>> put the relevant code, as described by the documentation
>> (http://docs.djangoproject.com/en/dev/topics/db/sql/#executing-custom-sql-directly>> ) into views.py?
>
> Oh, and: Another very logical place to put this is in a custom Manager
> subclass for the relevant Model; there's documentation on that usage in the
> docs:
>
>        
>http://docs.djangoproject.com/en/1.2/topics/db/managers/#adding-extra-manager-methods>

I've successfully got the data I was after, and I've even got it into
the template with success - thankyou!

The problem I now have is that the data returned has the format:

language,number,percent

where language = short language code.

In the template I'd like to represent language in it's long form, but
am having all types of trouble getting it to do so. I have the longs
in a dictionary in my models.py, in my views.py I've created a smaller
dict of long_langs, but how do I then match them in the template?

Here's some of my template attempts:

these proved that the data is coming through to the template
{{ target_stats }}
{# {{ long_langs }} #}
{# {{ lang_dict }} <br /> #}

These loops didn't work (for obvious reasons...)

{% for language, number, percent in target_stats %}
{{ long_lang.foorloop.counter }} {{ number }} {{ percent }} <br />
{% endfor %}

and

{% for language, number, percent in target_stats %}
{{ lang_dict }} {{ number }} {{ percent }} <br />
{% endfor %}

there have been a few other attempts - mostly riffs on those
themes...I even tried a
for(for(if))) but nothing came out.

I'm confused about how to get this right

cheers
L.

--
"... imagine a puddle waking up one morning and thinking, 'This is an
interesting world I find myself in - an interesting hole I find myself
in - fits me rather neatly, doesn't it? In fact it fits me
staggeringly well, must have been made to have me in it!' This is such
a powerful idea that as the sun rises in the sky and the air heats up
and as, gradually, the puddle gets smaller and smaller, it's still
frantically hanging on to the notion that everything's going to be
alright, because this world was meant to have him in it, was built to
have him in it; so the moment he disappears catches him rather by
surprise."
Douglas Adams

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#8 Dec. 9, 2010 03:23:27

Lachlan M.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

percentage of models given field value?


On Thu, Dec 9, 2010 at 14:13, Lachlan Musicman <data...@gmail.com> wrote:
> On Thu, Nov 25, 2010 at 16:00, Christophe Pettus <x...@thebuild.com> wrote:
>>
>> On Nov 24, 2010, at 8:13 PM, Lachlan Musicman wrote:
>>> Thanks Christopher. I've never done raw SQL in Django before. This may
>>> seem like a silly follow up question, but is it standard practice to
>>> put the relevant code, as described by the documentation
>>> (http://docs.djangoproject.com/en/dev/topics/db/sql/#executing-custom-sql-directly>>> ) into views.py?
>>
>> Oh, and: Another very logical place to put this is in a custom Manager
>> subclass for the relevant Model; there's documentation on that usage in the
>> docs:
>>
>>        
>>http://docs.djangoproject.com/en/1.2/topics/db/managers/#adding-extra-manager-methods>>
>
> I've successfully got the data I was after, and I've even got it into
> the template with success - thankyou!
>
> The problem I now have is that the data returned has the format:
>
> language,number,percent
>
> where language = short language code.
>
> In the template I'd like to represent language in it's long form, but
> am having all types of trouble getting it to do so. I have the longs
> in a dictionary in my models.py, in my views.py I've created a smaller
> dict of long_langs, but how do I then match them in the template?
>
> Here's some of my template attempts:
>
> these proved that the data is coming through to the template
> {{ target_stats }}
> {# {{ long_langs  }} #}
> {#  {{ lang_dict }} <br /> #}
>
> These loops didn't work (for obvious reasons...)
>
>  {% for language, number, percent in target_stats %}
>        {{ long_lang.foorloop.counter }} {{ number }} {{ percent }} <br />
>  {% endfor %}
>
> and
>
>  {% for language, number, percent in target_stats %}
>        {{ lang_dict }} {{ number }} {{ percent }} <br />
>  {% endfor %}
>
> there have been a few other attempts - mostly riffs on those
> themes...I even tried a
> for(for(if))) but nothing came out.
>
> I'm confused about how to get this right


Ignore - I got this within 5 minutes of posting. God.damn.it.



--
"... imagine a puddle waking up one morning and thinking, 'This is an
interesting world I find myself in - an interesting hole I find myself
in - fits me rather neatly, doesn't it? In fact it fits me
staggeringly well, must have been made to have me in it!' This is such
a powerful idea that as the sun rises in the sky and the air heats up
and as, gradually, the puddle gets smaller and smaller, it's still
frantically hanging on to the notion that everything's going to be
alright, because this world was meant to have him in it, was built to
have him in it; so the moment he disappears catches him rather by
surprise."
Douglas Adams

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

  • Root
  • » Django
  • » percentage of models given field value? [RSS Feed]

Board footer

Moderator control

Enjoy the 17th of October
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support