I have an app called “blog” that has a model called “Entry”. I use a class based generic to view this Entry and I am happy with this.
Now, along comes another app called “Eventapp” that has a model called “Event”. Now I want to query this model for a few events, and then send it to the class based generic mentioned above. How do I do this?
Here is what I have in my urls.py so far to view the Entry model:
urlpatterns = patterns('',
url(r'^$', ArchiveIndexView.as_view(
model=Entry, paginate_by=5, date_field='pub_date',template_name='homepage.html'),
),
url(r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\w{1,2})/(?P<slug>\w+)/$',
DetailView.as_view(model=Entry, queryset=Entry.objects.all(), slug_field='slug')),)
Is there a way to pass the Event model to ArchiveIndexView.as_view() and DetailView.as_view() somehow?
As it looks to me you would like to have your events on more than one page, a context processor may be the right tool for you. It should allow you to access a queryset of events within the context of every template:
Because of the queryset’s laziness you don’t have to be afraid to hit the database if you don’t need the list, the query is only run if you would iterate over
events.Of course you could also eg. subclass the views and add the events there to the context, but if more than one view is affected a context processor might make more sense!