sorting an internal GenericRelation field in Django -
i have model item with:
class item(models.model): comments = generic.genericrelation(comment) ...
my comment model :
class comment(models.model): """ comment object model want integrate comment engine """ content_type = models.foreignkey(contenttype) object_id = models.positiveintegerfield() content_object = generic.genericforeignkey('content_type', 'object_id') comment = models.textfield(max_length=512) user = models.foreignkey( user ) created_date = models.datetimefield(auto_now_add=true)
in templete have , set in templete context items item.objects.all():
{% item in items %} {%for comment in item.comments.all %} <p> comment.comment</p> {%endfor%} {%endfor%}
what comments sorted created_date per item. how can set how sort internal genericrelation?
use model's meta class
class comment(models.model): # attrs class meta: ordering = ['created_date']
or use manager verbose in case
class commentmanager(models.manager): def get_queryset(self): return super(commentmanager, self).get_queryset().order_by('created_date') class comment(models.model): # attrs objects = commentmanager()
more on managers across relationships: https://docs.djangoproject.com/en/dev/topics/db/managers/#controlling-automatic-manager-types
Comments
Post a Comment