programmatic method to get a list of "published" images in wagtail?

Viewed 459

I'm playing with wagtail and considering using it for a site. In the process I'm looking into serving the site through a combination of django-storages and wagtail-bakery.

At the moment I'm trying to get the images served and want to obtain a list of wagtail images that are considered "published".

I looked at the properties of the Image model, but I don't see anything that sticks out as a way to define if the Image is published or not.

Is there a way to determine if an Image is published or not, or get a list of Images belonging to publshed pages?

>>> from wagtail.images.models import Image
>>> 
>>> img = Image.objects.all()[0]
>>> for i in sorted(dir(img)):
...     print(i)
... 
DoesNotExist
Meta
MultipleObjectsReturned
__class__
__delattr__
__dict__
__dir__
__doc__
__eq__
__format__
__ge__
__getattribute__
__getstate__
__gt__
__hash__
__init__
__init_subclass__
__le__
__lt__
__module__
__ne__
__new__
__reduce__
__reduce_ex__
__repr__
__setattr__
__setstate__
__sizeof__
__str__
__subclasshook__
__weakref__
_check_column_name_clashes
_check_constraints
_check_field_name_clashes
_check_fields
_check_id_field
_check_index_together
_check_indexes
_check_local_fields
_check_long_column_names
_check_m2m_through_same_relationship
_check_managers
_check_model
_check_model_name_db_lookup_clashes
_check_ordering
_check_property_name_related_field_accessor_clashes
_check_search_fields
_check_single_primary_key
_check_swappable
_check_unique_together
_do_insert
_do_update
_get_FIELD_display
_get_next_or_previous_by_FIELD
_get_next_or_previous_in_order
_get_pk_val
_get_unique_checks
_has_field
_meta
_perform_date_checks
_perform_unique_checks
_save_parents
_save_table
_set_file_hash
_set_pk_val
_state
admin_form_fields
check
clean
clean_fields
collection
collection_id
created_at
date_error_message
default_alt_text
delete
file
file_hash
file_size
filename
focal_point_height
focal_point_width
focal_point_x
focal_point_y
from_db
full_clean
get_autocomplete_search_fields
get_deferred_fields
get_file_hash
get_file_size
get_filterable_search_fields
get_focal_point
get_indexed_instance
get_indexed_objects
get_next_by_created_at
get_previous_by_created_at
get_rect
get_rendition
get_rendition_model
get_search_fields
get_searchable_search_fields
get_suggested_focal_point
get_upload_to
get_usage
get_willow_image
has_focal_point
height
id
indexed_get_content_type
indexed_get_parent
indexed_get_toplevel_content_type
is_editable_by_user
is_landscape
is_portrait
is_stored_locally
objects
open_file
pk
prepare_database_save
refresh_from_db
renditions
save
save_base
search_fields
serializable_value
set_focal_point
tagged_items
tags
title
unique_error_message
uploaded_by_user
uploaded_by_user_id
usage_url
validate_unique
width




1 Answers

There is no 'published' attribute because Image instances are not part of a workflow. Draft, publish, and un-publish are terms that belong to a Page.

Image does have a get_usage method.

get_usage lives in wagtail.images.models.AbstractImage.get_usage and calls wagtail.admin.models.get_object_usage. This doc string reads:

"Returns a queryset of pages that link to a particular object"

Unfortunately, this will only give you images that are referenced via a database relation. It will NOT give you images used in streamfields (json). This also doesn't say much about the workflow state the related Page is in.

You might want to have a look at https://github.com/cfpb/wagtail-inventory

Related