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.
By default, the
AnyUrlFieldonly supports linking to external pages. To add support for your own models (e.g. anArticlemodel), include the following code inmodels.py:from any_urlfield.models import AnyUrlField AnyUrlField.register_model(Article)
Now, the
AnyUrlFieldoffers users a dropdown field to directly select an article. By default, it uses adjango.forms.ModelChoiceFieldfield with adjango.forms.Selectwidget to render the field. This can be customized using theform_fieldandwidgetparameters:from any_urlfield.models import AnyUrlField from any_urlfield.forms import SimpleRawIdWidget AnyUrlField.register_model(Article, widget=SimpleRawIdWidget(Article))
Now, the
Articlemodel will be displayed as raw input field with a browse button.-
formfield(**kwargs)¶ Return a django.forms.Field instance for this field.
-
get_prep_value(value)¶ Perform preliminary non-db specific value checks and conversions.
-
pre_save(model_instance, add)¶ Return field’s value just before saving.
-
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.
-
to_python(value)¶ Convert the input value into the expected Python data type, raising django.core.exceptions.ValidationError if the data can’t be converted. Return the converted value. Subclasses should override this.
-
validate(value, model_instance)¶ Validate value and raise ValidationError if necessary. Subclasses should override this to provide validation logic.
-
value_to_string(obj)¶ Return a string value of this field from the passed obj. This is used by the serialization framework.
-
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 str(value)
A conversion to
strorstrcauses 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
- an external URL:
-
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/orpageid://22.
-
type_prefix¶ Return the URL type prefix. For external URLs this is always
"http".
-

