any_urlfield.models

The AnyUrlField class

class any_urlfield.models.AnyUrlField(*args, **kwargs)

A CharField that can either refer to a CMS page ID, or external URL.

AnyUrlField, with external URL input.
AnyUrlField, with internal page input.

By default, the AnyUrlField only supports linking to external pages. To add support for your own models (e.g. an Article model), include the following code in models.py:

from any_urlfield.models import AnyUrlField
AnyUrlField.register_model(Article)

Now, the AnyUrlField offers users a dropdown field to directly select an article. By default, it uses a django.forms.ModelChoiceField field with a django.forms.Select widget to render the field. This can be customized using the form_field and widget parameters:

from any_urlfield.models import AnyUrlField
from any_urlfield.forms import SimpleRawIdWidget

AnyUrlField.register_model(Article, widget=SimpleRawIdWidget(Article))

Now, the Article model will be displayed as raw input field with a browse button.

classmethod register_model(ModelClass, form_field=None, widget=None, title=None, prefix=None)

Register a model to use in the URL field.

This function needs to be called once for every model that should be selectable in the URL field.

Parameters:
  • ModelClass – The model to register.
  • form_field – The form field class used to render the field. This can be a lambda for lazy evaluation.
  • widget – The widget class, can be used instead of the form field.
  • title – The title of the model, by default it uses the models verbose_name.
  • prefix – A custom prefix for the model in the serialized database format. By default it uses “appname.modelname”.
classmethod resolve_objects(objects, skip_cached_urls=False)

Make sure all AnyUrlValue objects from a set of objects is resolved in bulk. This avoids making a query per item.

Parameters:
  • objects – A list or queryset of models.
  • skip_cached_urls – Whether to avoid prefetching data that has it’s URL cached.

The AnyUrlValue class

class any_urlfield.models.AnyUrlValue(type_prefix, type_value, url_type_registry=None)

Custom value object for the AnyUrlField. This value holds both the internal page ID, and external URL. It can be used to parse the database contents:

value = AnyUrlValue.from_db_value(url)
article = value.get_object()
print unicode(value)

A conversion to unicode or str causes the URL to be generated. This allows the field value to be used in string concatenations, or template variable evaluations:

{{ mymodel.url }}
bound_type_value

Keep a reference to the actual object. This is a trick for ForeignKeyRawIdWidget, which only receives the integer value. Instead of letting it resolve the object, pass the prefetched object here.

exists()

Check whether the references model still exists.

classmethod from_db_value(url, url_type_registry=None)

Convert a serialized database value to this object.

The value can be something like:

  • an external URL: http://.. , https://..
  • a custom prefix: customid://214, customid://some/value
  • a default “app.model” prefix: appname.model://31
classmethod from_model(model, url_type_registry=None)

Convert a model value to this object.

get_model()

Return the model that this value points to.

get_object()

Return the database object that the value points to.

classmethod resolve_values(values, skip_cached_urls=False)

Resolve the models for collection of AnyUrlValue objects, to avoid a query per object.

to_db_value()

Convert the value into a serialized format which can be stored in the database. For example: http://www.external.url/ or pageid://22.

type_prefix

Return the URL type prefix. For external URLs this is always "http".