wagner-felix
wagner-felix

Reputation: 875

Django Generic Detail view and limiting objects

Here are my urls.py, template and model. What I want to know is how I can limit how many entries of a field will be shown.

Urls.py

a_detail = {
    'queryset': A.objects.all(),
    'template_object_name' : "b",
    'slug_field': 'a',
    'template_name': 'a/detail.html',
}

urlpatterns = patterns('',
    (r'^detail/(?P<slug>[a-zA-Z0-9_.-]+)/$', list_detail.object_detail, a_detail),
)

in my template:

{% for log in b.logs.all %}
     <tr>
         <td>{{ log.timestamp }}</td>
         <td>{{ log.message }}</td>
     </tr>

 {% empty %}
      <tr>
         <td>Kein Timestamp vorhanden</td>
         <td>Keine Log Message vorhanden</td>
      </tr>
 {% endfor %}

My Models:

class A(models.Model):
    logs = models.ManyToManyField(Log, through='Timestamps', editable=False)

Class Log(models.Model):
    message = models.TextField()
    timestamp = models.DateTimeField(auto_now=True) 

Is it possible to only show the 20 latest log messages?

Upvotes: 0

Views: 382

Answers (1)

Hedde van der Heide
Hedde van der Heide

Reputation: 22459

You can set a query limit like so:

Model.objects.all()[X:Y]

Or you can use paginate on your listView class

paginate_by = X

Upvotes: 1

Related Questions