Display objects

The ListView and DetailView views have an enhanced fields attribute.

  • The list view defaults to just show the object string; set it to None to show all fields

  • The default view defaults to show all fields

  • The fields attribute is normally a list of strings for the field names

  • Enhanced display fields also support a DisplayValue instance

Fastview provides the following DisplayValue types:

  • AttributeValue - show an attribute of the object. The following are equivalent:

    fields = ["name"]
    fields = [AttributeValue("name")]
    

    An AttributeValue can also take a label, eg AttributeValue("user", label="Name")

  • ObjectValue - convert the object to a string using str(object)

Create a custom display value by subclassing one of those or the base DisplayValue class.

API reference

class fastview.views.display.DisplayValue

Bases: object

Abstract base class for all display values

get_label(view: DisplayFieldMixin) str
Parameters

view – The view class rendering this value

Returns

The label for this field

Return type

str

get_order_by(view: DisplayFieldMixin) str
Parameters

view – The view class rendering this value

Returns

Field name for order_by(..)

Return type

str

get_slug(view: DisplayFieldMixin) str
Parameters

view – The view class rendering this value

Returns

A slug for this field, for use in ordering

Return type

str

get_value(view: DisplayFieldMixin, instance: Model) Any
Parameters
  • view – The view class rendering this value

  • instance – The model instance for this value

Returns

The value as it will be passed to the template

Return type

Any

If this returns a string of valid sanitised HTML, mark it with mark_safe for it to be rendered without further escaping.

class fastview.views.display.AttributeValue(attribute, label=None, order_by=None)

Bases: fastview.views.display.DisplayValue

Display an attribute of the object

get_label(view: DisplayFieldMixin) str
Parameters

view – The view class rendering this label

Returns

the label for this value

Return type

str

Priority is given to a label set by __init__()

If not set, it will honour Django’s admin syntax by looking for a short_description attribute on the view’s model, eg:

class MyModel(models.Model):
    full_name = models.CharField(max_length=255)

    def get_first_name(self):
        return full_name.split(" ")[0]

    get_first_name.short_description = "First name")

If that is not found, it will title case the attribute name then replace _ with a space eg:

value = AttributeValue("full_name")
# value.get_label(view) will return "Full name"
get_order_by(view: DisplayFieldMixin) str
Parameters

view – The view class rendering this label

Returns

Field name for order_by(..)

Return type

str

Priority is given to an ``order_by set by __init__().

If not set, it will honour Django’s admin syntax by looking for a admin_order_field attribute on the attribute on the view’s model, eg:

class MyModel(models.Model):
    foo = models.IntegerField()

    def bar(self):
        return self.foo + 1

    bar.admin_order_field = "foo"

If not set, it will default to the attribute itself, eg:

value = AttributeValue("foo")
# value.get_order_by(view) will return "foo"
get_value(view: DisplayFieldMixin, instance: Model) Any
Parameters
  • view – The view class rendering this value

  • instance – The model instance for this value

Returns

The attribute value from the instance

Return type

Any

class fastview.views.display.ObjectValue

Bases: fastview.views.display.DisplayValue

Return the string representation of the object

get_label(view: DisplayFieldMixin) str
Parameters

view – The view class rendering this value

Returns

The label for this field

Return type

str

get_order_by(view: DisplayFieldMixin) str
Parameters

view – The view class rendering this value

Returns

Field name for order_by(..)

Return type

str

get_value(view: DisplayFieldMixin, instance: Model) Any
Parameters
  • view – The view class rendering this value

  • instance – The model instance for this value

Returns

The value as it will be passed to the template

Return type

Any

If this returns a string of valid sanitised HTML, mark it with mark_safe for it to be rendered without further escaping.