def _filter_choices(self):
"""
Removes the ContentType objects that have not
the 'gallery_visible' attribute set to True.
"""
filtered_choices = []
# initially self.choices contains all content types
for choice in self.choices:
try:
if choice[0] == "":
# add the empty choice
filtered_choices.append(choice)
continue
# get the content type and determine the model
ctype = ContentType.objects.get(pk=int(choice[0]))
model_class = ctype.model_class()
if model_class.gallery_visible:
# add the choice if the model has the 'gallery_visible'
# attribute and it's set to True
filtered_choices.append(choice)
except:
# the model has not the 'gallery_visible' attribute
pass
# replace original choices
self.choices = filtered_choices
评论列表
文章目录