我找到了使用此处编写的博客文章应用程序Django
的代码:我对这段代码的作用感到困惑:请解释下面的 CommentManager 类的作用以及传递给它的参数如何分配给某些“任意键”。object
在下面的课程中也使用相同的方法Comment
。那是怎么用的?
class CommentManager(models.Manager):
def comments_for_object(self, site_id, app_label, model, obj_id, comment_type='comment'):
''' Get a QuerySet of all public comments for the specified object. '''
kwargs = {
'site__id__exact': site_id,
'content_type__app_label__exact': app_label,
'content_type__model__exact': model,
'object_id__exact': obj_id,
'comment_type__exact': comment_type,
'is_public__exact': True,
}
return self.filter(**kwargs)
class Comment(models.Model):
''' Data model for both comments and trackbacks '''
objects = CommentManager()
content_type = models.ForeignKey(ContentType)
object_id = models.IntegerField(_('object ID'))
comment = models.TextField(_('comment'), max_length=3000)
submit_date = models.DateTimeField(_('date/time submitted'), auto_now_add=True)
is_public = models.BooleanField(_('is public'))
ip_address = models.IPAddressField(_('ip address'))
site = models.ForeignKey(Site)
typeChoices = (
('comment', 'Comment'),
('trackback', 'Trackback'),
)