views:

141

answers:

1

Hi guys, im using generic view, i would like to update a field (most_view) in another database table.

How ill update or create a new register for "most view" when the user are reading a article?

ulrs.py

from Paso_a_Paso.noticias.models import Noticia

noticias_info_dict = {
        'queryset':Noticia.objects.all(),
        'date_field':'pub_date',
}

urlpatterns = patterns('django.views.generic.date_based',
    (r'^$','archive_index', noticias_info_dict,'noticias_archive_index'),
    (r'^(?P<year>\d{4})/$','archive_year', noticias_info_dict,'noticias_archive_year'),
    (r'^(?P<year>\d{4})/(?P<month>\w{3})/$','archive_month', noticias_info_dict,'noticias_archive_month'),
    (r'^(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{2})/$','archive_day', noticias_info_dict,'noticias_archive_day'),
    (r'^(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{2})/(?P<slug>[-\w]+)/$','object_detail', noticias_info_dict,'noticias_archive_detail'),
)

Thanks guys :)

A: 

There's no real clean way to do this with the current generic views (class-based generic views will help).

You can:

  • Create a wrapper view that calls the generic view and does the other update. The disadvantage here is that you'll duplicate part of the work of the generic view (finding the right article object), which also results in duplicate DB queries.

or

  • Abandon the generic view and write your own. Not as hard as you might think, and probably the cleanest approach for now.
Carl Meyer
If he is passing the `noticias_info_dict` with queryset now, why would a wrapper create duplicate queries? I don't think it would at all. I think a wrapper function is perfectly acceptable.
anonymous coward
Querysets are lazy, so creating that queryset in the info dict does not trigger a query until the generic view calls the .get() method to fetch the single row requested. If you have to call .get() in your wrapper, then the generic view calls it again (which you can't control), that's two queries.
Carl Meyer