Question

Is there a way to find a page (query) which contains an specific content type.

Lets say I have a page (logout) with content type LogoutFormContent.

How can I find the page(s) that has LougOutFormContent as content type.

I tried to do this:

page = Page.objects.filter(logoutformcontent_set=True)

which generates the following query

SELECT * FROM `page_page` INNER JOIN `page_page_logoutformcontent` 
ON (`page_page`.`id` = `page_page_logoutformcontent`.`parent_id`) 
WHERE (`page_page_logoutformcontent`.`id` = 1) 

This is almost correct except page_page_logoutformcontent.id = 1???

Était-ce utile?

La solution

Using this function to find out which page(s) has a certain/given content type

def get_page_for_ct(request, ct):
    ctp_page = Page.content_type_for(ct)
    page = ctp_page.objects.filter(parent__language=request.LANGUAGE_CODE)
    if not page:
        raise ImproperlyConfigured(
                'page not found: "%s" for language "%s" '
                % (ct, request.LANGUAGE_CODE))
    return page[0].parent
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top