如何在我传递它们的情况下对元素进行排序" get_object_or_404"来自观点

时间:2013-06-14 15:02:14

标签: django django-models django-templates django-views

我想通过“anno”订购“regista”({% for film in regista.film_set.all %})和“续集”({% for sequel in film.sequels.all %})的“电影”,但我不知道我该怎么做.. ..谢谢你的帮助...

这里是models.py

class Anno( models.Model ):
    anno = models.CharField( max_length=4 )
    def __unicode__(self):
        return self.anno
    class Meta:
        verbose_name_plural = "Anni"

class Film( models.Model ):
    titolo = models.CharField( max_length=39 )
    trama = models.TextField( max_length=1000 )
    anno = models.ForeignKey( Anno )
    registi = models.ManyToManyField( Regista )
    generi = models.ManyToManyField( Genere_Film )
    sequels = models.ManyToManyField( "self", blank=True )
    def __unicode__(self):
        return self.titolo + " " + self.trama
    class Meta:
        verbose_name_plural = "Films"

这里是views.py

def film(request, id):
    film = get_object_or_404( Film, pk=id )
    return render_to_response('Film.html', { 'film': film })

这里是Film.html

{{ film.titolo }}
{% for generi in film.generi.all %}{{ generi.genere}}{% endfor %}
{{ film.anno }}
{{ film.trama }}
{% for regista in film.registi.all %}
  {{ regista.nome }} {{ regista.cognome }}
  {% for film in regista.film_set.all %}
    {{ film.titolo }}
  {% endfor %}
{% endfor %}
{% for sequel in film.sequels.all %}
  {{ sequel.titolo }}
{% endfor %}

1 个答案:

答案 0 :(得分:0)

class Meta中,您可以指定ordering

class Anno( models.Model ):
    anno = models.CharField( max_length=4 )
    def __unicode__(self):
        return self.anno
    class Meta:
        verbose_name_plural = "Anni"
        ordering = ('anno', )