Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6824213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:52:30+00:00 2026-05-26T21:52:30+00:00

Supose a student attandance system. For a student and a course we have N:M

  • 0

Supose a student attandance system.
For a student and a course we have N:M relation named attandance.
Also whe have a model with attandances status (present, absent, justified, …).

level( id, name, ... )
student ( id, name, ..., id_level )
course( id, name, ... )
status ( id, name, ...)  #present, absemt, justified, ...
attandance( id, id_student, id_course, id_status, date, hour )
   unique_together = ((id_student, id_course, id_status, date, hour),)

I’m looking for a list of students with >20% of absent for a level sorted by %. Something like:

present = status.objects.get( name = 'present')
justified = status.objects.get( name = 'justified')
absent = status.objects.get( name = 'absent')

#here the question. How to do this:
Student.objects.filter( level = level ).annotate( 
         nPresent =count( attandence where status is present or justified ),
         nAbsent =count( attandence where status is absent ),
         pct = nAbsent / (nAbsent + nPresent ),
      ).filter( pct__gte = 20 ).order_by( "-pct" )

If it is not possible to make it with query api, any workaround (lists, sets, dictionaris, …) is wellcome!

thanks!

.

.

.

—- At this time I have a dirty raw sql writed by hand ————————–

select 
                   a.id_alumne, 
                   coalesce ( count( p.id_control_assistencia ), 0 ) as p,
                   coalesce ( count( j.id_control_assistencia ), 0 ) as j,
                   coalesce ( count( f.id_control_assistencia ), 0 ) as f,
                   1.0 * coalesce ( count( f.id_control_assistencia ), 0 ) /
                   ( coalesce ( count( p.id_control_assistencia ), 0 ) + coalesce ( count( f.id_control_assistencia ), 0 ) ) as tpc                   
                from 
                   alumne a 

                   inner join
                   grup g
                       on (g.id_grup = a.id_grup )

                   inner join
                   curs c
                       on (c.id_curs = g.id_curs)

                   inner join
                   nivell n
                       on (n.id_nivell = c.id_nivell)

                   inner join 
                   control_assistencia ca 
                       on (ca.id_estat is not null and 
                           ca.id_alumne = a.id_alumne )

                   inner join
                   impartir i
                       on ( i.id_impartir = ca.id_impartir )

                   left outer join 
                   control_assistencia p
                       on ( 
                           p.id_estat in ( select id_estat from estat_control_assistencia where codi_estat in ('P','R' ) ) and
                           p.id_control_assistencia = ca.id_control_assistencia )

                   left outer join 
                   control_assistencia j
                       on ( 
                           j.id_estat = ( select id_estat from estat_control_assistencia where codi_estat = 'J' ) and
                           j.id_control_assistencia = ca.id_control_assistencia )

                   left outer join 
                   control_assistencia f
                       on ( 
                           f.id_estat = ( select id_estat from estat_control_assistencia where codi_estat = 'F' ) and
                           f.id_control_assistencia = ca.id_control_assistencia )

                where 
                    n.id_nivell = {0} and
                    i.dia_impartir >= '{1}' and
                    i.dia_impartir <= '{2}'

                group by 
                   a.id_alumne

                having
                   1.0 * coalesce ( count( f.id_control_assistencia ), 0 ) /
                   ( coalesce ( count( p.id_control_assistencia ), 0 ) + coalesce ( count( f.id_control_assistencia ), 0 ) )
                   > ( 1.0 * {3} / 100)
                order by
                   1.0 * coalesce ( count( f.id_control_assistencia ), 0 ) /
                   ( coalesce ( count( p.id_control_assistencia ), 0 ) + coalesce ( count( f.id_control_assistencia ), 0 ) )
                   desc   
                '''.format( nivell.pk, data_inici, data_fi, tpc   )
  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:52:31+00:00Added an answer on May 26, 2026 at 9:52 pm

    If you don’t care too much whether it uses the query api or python after the fact, use itertools.groupby.

    attendances = Attendance.objects.select_related().filter(student__level__exact=level)
    students = []
    for s, g in groupby(attendances, key=lambda a: a.student.id):
        g = list(g) # g is an iterator
        present = len([a for a in g if a.status == 'present'])
        absent = len([a for a in g if a.status == 'absent'])
        justified = len([a for a in g if a.status == 'justified'])
        total = len(g)
        percent = int(absent / total)
        students.append(dict(name=s.name, present=present, absent=absent, percent=percent))
    students = (s for s in sorted(students, key=lambda x: x['percent']) if s['percent'] > 25)
    

    You can pass the resulting list of dicts to the view the same way you would any other queryset.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Supose we have a very simple model: Station has at least one Train Train
Supose I have following entities created from database tables: Person Student Student include Person
Suppose that I have a database with student information: {'student_name' : 'Alen', 'subjects' :
Supose I have a matrix. This matrix is blank except some points that create
Supose I have this string: a= hello world hella warld and I want to
Supose i have name Mink,Mark,Aashis . How do compare them and arrange them according
Suppose I have three models: Student , SchoolClass , and DayOfWeek . There is
suppose we have a vector<student> allstudent Now I would like to sort the students
Suppose: I have a single table that holds the book ID, student ID and
Suppose I have the following two tables: STUDENT studentid lastname firstname 1 Smith John

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.