(List objects(Recently Added - Most Common

12 views
Skip to first unread message

mohamed wagdy

unread,
Apr 25, 2012, 5:47:55 PM4/25/12
to Django users
please i want a help
________________
I have some object stored in a database and i want to
make List of this object (Recently Added object -
and
Most Common)

Bill Freeman

unread,
Apr 26, 2012, 10:48:59 AM4/26/12
to django...@googlegroups.com
And I want a better description of what you are trying to do.

For recently added, do you want the last N added, or do you want
those added within the last D days, or do you want a combination.

I have no idea what you mean by "Most Common".

mohamed wagdy

unread,
Apr 26, 2012, 11:39:42 AM4/26/12
to django...@googlegroups.com
recently mean N added

and

most common it mean the N object that the person used

say . i make an event system and i want to know
most common event in my system
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> 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.
>
>


--
* **mohamed wagdy *

Bill Freeman

unread,
Apr 26, 2012, 12:33:41 PM4/26/12
to django...@googlegroups.com
For most recent N, try something like:

My_Model.objects.order_by("-id")[:N]

[This assumes that your database assures that primary keys increase
monotonically against creation order. This is usual. If you have an
unusual database in this regard, or if you wanted to limit how old an
item can be considered "Recently Added", you would need to add a
DateTimeField with auto_now_add set to True, and work with that.]


I still don't follow what you want for Most Common, but I'll guess
that you want, for example, an "Event" model that has a CharField,
called, for example "etype", and you want to know which value of etype
occurs most among the existing model instances. There might be a way
to do this through the ORM, maybe using aggregate, annotate, etc., but
I'd be reaching for my SQL reference at this point to do a GROUP BY on
the etype field, then do a COUNT on the groups, then ORDER BY the
result of the COUNT, and finally LIMIT the result returned to 1. The
exact syntax, whether things need to be set up as subqueries, and
behavior may vary depending on which database is used. You should be
able to do this with "raw" queryset method, and you might be able to
do it with the "extra" queryset method, but you can always reach
around the ORM if needed. The problem with some simpler approaches
through the ORM is that, if the table is large, you would like to
limit how many results are transferred from the database to django.
Reply all
Reply to author
Forward
0 new messages