本文整理汇总了Python中django.contrib.contenttypes.models.ContentType.get_object_for_this_type方法的典型用法代码示例。如果您正苦于以下问题:Python ContentType.get_object_for_this_type方法的具体用法?Python ContentType.get_object_for_this_type怎么用?Python ContentType.get_object_for_this_type使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.contrib.contenttypes.models.ContentType
的用法示例。
在下文中一共展示了ContentType.get_object_for_this_type方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: mooclet_create
# 需要导入模块: from django.contrib.contenttypes.models import ContentType [as 别名]
# 或者: from django.contrib.contenttypes.models.ContentType import get_object_for_this_type [as 别名]
def mooclet_create(request, **kwargs):
'''
create a new mooclet
required kwargs: quiz, type, parent object
'''
quiz = get_object_or_404(Quiz,pk=kwargs['quiz_id'])
mooclet_type = get_object_or_404(MoocletType,name=kwargs['type'])
# object class that the mooclet is attached to
parent_content_type = mooclet_type.content_type
parent_content = ContentType.get_object_for_this_type(parent_content_type, pk=kwargs[parent_content_type.name+'_id'])
if request.method == 'GET':
mooclet_form = MoocletForm(initial={'type':mooclet_type})
context = {
'quiz':quiz,
'mooclet_form':mooclet_form,
}
if 'question' in kwargs:
context['question'] = get_object_or_404(Question,pk=kwargs['question_id'])
if 'answer' in kwargs:
context['answer'] = get_object_or_404(Answer,pk=kwargs['answer_id'])
return render(request, 'engine/mooclet_create.html', context)
elif request.method == 'POST':
mooclet_form = MoocletForm(request.POST)
mooclet = mooclet_form.save()
prev_url = request.POST['prev']
return redirect(prev_url)
示例2: mooclet_detail
# 需要导入模块: from django.contrib.contenttypes.models import ContentType [as 别名]
# 或者: from django.contrib.contenttypes.models.ContentType import get_object_for_this_type [as 别名]
def mooclet_detail(request, **kwargs):
'''
mooclet home page
required kwargs: quiz, mooclet, and all parent objects
'''
quiz = get_object_or_404(Quiz,pk=kwargs['quiz_id'])
mooclet = get_object_or_404(Mooclet,pk=kwargs['mooclet_id'])
# look up mooclet type and identify associated parent object
# object class that the mooclet is attached to
parent_content_type = mooclet.type.parent_content_type
parent_content = ContentType.get_object_for_this_type(parent_content_type, pk=kwargs[parent_content_type.name+'_id'])
# populate a mooclet context dict
mooclet_context = {}
if parent_content_type.name == 'question':
mooclet_context['question'] = parent_content
if parent_content_type.name == 'answer':
mooclet_context['answer'] = parent_content
mooclet_context['question'] = parent_content.question
versions = mooclet.version_set.all()
version_content_type = ContentType.objects.get_for_model(Version)
mooclet_policy_form = MoocletPolicyForm(instance=mooclet)
# page used to display variables that are version-specific
if request.method == 'GET':
context = {
'quiz':quiz,
'mooclet':mooclet,
'versions':versions,
'mooclet_policy_form':mooclet_policy_form,
}
# pass additional context variables for navigation
context.update(mooclet_context)
if 'question' in kwargs:
context['question'] = get_object_or_404(Question,pk=kwargs['question_id'])
if 'answer' in kwargs:
context['answer'] = get_object_or_404(Answer,pk=kwargs['answer_id'])
return render(request, 'engine/mooclet_detail.html',context)
elif request.method == 'POST':
# process mooclet policy form for adjusting policy
mooclet_policy_form = MoocletPolicyForm(request.POST, instance=mooclet)
mooclet = mooclet_policy_form.save()
# converts dict of objs to dict of pks
mooclet_context_pk = {name+'_id':obj.pk for name,obj in mooclet_context.items()}
return redirect('engine:mooclet_detail', quiz_id=quiz.pk, mooclet_id=mooclet.pk, **mooclet_context_pk)
示例3: get_mooclet_context
# 需要导入模块: from django.contrib.contenttypes.models import ContentType [as 别名]
# 或者: from django.contrib.contenttypes.models.ContentType import get_object_for_this_type [as 别名]
def get_mooclet_context(mooclet):
# content_type for model that the mooclet is attached to
parent_content_type = mooclet.type.content_type
# parent object instance
parent_content = ContentType.get_object_for_this_type(parent_content_type, pk=parent_content_id)
if parent_content_type.name == 'question':
question = parent_content
elif parent_content_type.name == 'answer':
answer = parent_content
question = answer.question