> On 9/21/07, Yuri Baburov <[EMAIL PROTECTED]> wrote:
> > I like newforms-admin very much, because it's customizeable.
> > But now imagine that you have 10 usual models in admin interface, but
> > 11th needs to be special.
> > One possible example: you need to show, how much LogEntries for each
> > model was created by each user for some date interval.
> > What you are going to do?
>
> Erm, how about:
>
> 1. Register the first 10 models with a stock ModelAdmin class, which
> you can do in one fell swoop.
> 2. Write a custom class for the 11th, and register it with that.
> 3. Profit!

Maybe I explained incorrectly a bit.
I have no Model class, but want to have all that ModelAdmin could give me.

--------------------------warning: very cruel hacks
below------------------------------

All I have is my very own queryset, i.e.
    mode = 'day' # for example
    model = User # for example
    ct = ContentType.objects.get_for_model(model).id
    field = LogEntry._meta.get_field('action_time')
    db_type = field.db_type
    dt = lambda k, v: get_where_clause(k, '', 'action_time', v, db_type)
    params = [ct]
    for k,v in filters:
        params.extend(field.get_db_prep_lookup(k, v))
    query = """
        SELECT user_id,
            DATE_TRUNC('%s', action_time) as date,
            count(distinct object_id)
        FROM django_admin_log
        WHERE %s
        GROUP BY user_id, date
    """ % (mode,
        " AND ".join(
            ['content_type_id=%s']+[dt(k,v) for k,v in filters]
        ))
    items = get_custom_sql(query, ['user_id', 'date', 'number'], params)
    ...
Currently the easiest solution I found looks like this:

@page('admin/timing/actions/change_list.html')
def view_actions(request):
    self = ActionAdmin(Action, admin.site.root)
    try:
        cl = ChangeList(request, self.model, self.list_display,
self.list_display_links, self.list_filter, self.date_hierarchy,
self.search_fields, self.list_select_related, self.list_per_page,
self)
    except IncorrectLookupParameters:
        # Wacky lookup parameters were given, so redirect to the main
        # changelist page, without parameters, and pass an 'invalid=1'
        # parameter via the query string. If wacky parameters were given and
        # the 'invalid=1' parameter was already in the query string, something
        # is screwed up with the database, so display an error page.
        if ERROR_FLAG in request.GET.keys():
            return render_to_response('admin/invalid_setup.html',
{'title': _('Database error')})
        return HttpResponseRedirect(request.path + '?' + ERROR_FLAG + '=1')

    filters = [(k[6:],str(v[0])) for k,v in request.GET.iteritems() if
k.startswith('hour__')]
    mode, dates, grid, minmax = get_grid(filters)
    if mode == 'week':
        dates = [(d, d+datetime.timedelta(6)) for d in dates]
    return {
        'title': cl.title,
        'is_popup': cl.is_popup,
        'cl': cl,
        'mode': mode,
        'dates': dates,
        'grid': grid,
        'minmax': minmax,
    }

Here Action is model without actual data, declarated as the following:

class Action(Model):
    entry = ForeignKey(LogEntry)
    action = CharField(max_length=15, choices = ACTION_LIST)
    content_type = ForeignKey(ContentType)
    date = DateTimeField()

class ActionAdmin(ModelAdmin):
    list_filter = ['date']
    pass

-- 
Best regards, Yuri V. Baburov, ICQ# 99934676, Skype: yuri.baburov,
MSN: [EMAIL PROTECTED]

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-developers@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to