{8L: {33L: <Author: Poe>}, 21L: {32L: <Book: The Great Gatsby>, 7L: <Book: Great Expectations>}, 22L: {7L: <Quote: Hamlet>}}
을 8 인 경우 :이 읽기 내 템플릿 객체를 전달
def myfavorites(request):
queryset = Favorite.objects.favorites_for_user(user=request.user).select_related('content_type', 'content_object')
generics = {}
for item in queryset:
generics.setdefault(item.content_type_id, set()).add(item.object_id)
content_types = ContentType.objects.in_bulk(generics.keys())
relations = {}
for ct, fk_list in generics.items():
ct_model = content_types[ct].model_class()
relations[ct] = ct_model.objects.in_bulk(list(fk_list))
for item in queryset:
setattr(item, '_content_object_cache',
relations[item.content_type.id][item.object_id])
return render_to_response('myfavorites.html', {
'favorites':relations
},
context_instance=RequestContext(request))
을 저자 테이블의 ID 21은 My Book 테이블의 ID이고, 22는 My Quote 테이블의 ID입니다.
나는 그것을 읽는 있도록 객체 목록을하기 위해 노력하고있어 :
포
책
위대한 개츠비
큰 기대
저자
제품 번호 :
햄릿은 어떻게 장고 템플릿에서이 객체를 구문 분석? 당신은 쉽게 이런 식으로 뭔가를 할 수있는, 그런
:
뷰에서 개체를 변형 할 수 있습니까, 아니면 순수한 템플릿 솔루션이어야합니까? – SingleNegationElimination
원래보기를 표시하도록 편집했습니다. 보기를 변형 시키는데 도움이 되었습니까? –
내 답변 맨 아래에보기에서 개체 (관계)를 구문 분석하고 템플릿으로 보낼 개체를 분할 할 수 있어야한다고 나와 있습니다. render_to_response ('myfavorites.html', { 'authors': relations [8], 'books': 관계 [21], 'quotes': relations [22] }와 같은 render_to_response 호출에서이 작업을 수행 할 수 있습니다. , '8L에 8을 사용할 수 있는지 확인해야 할 것입니다. 8L 21L과 22L로 변환해야 할 수도 있습니다 [8/21/22] 관계의 일부 반환. – Furbeenator