django_evolution.mock_models

Utilities for building mock database models and fields.

Functions

create_field(project_sig, field_name, ...[, ...])

Create a Django field instance for the given signature data.

Classes

MockMeta(project_sig, app_name, model_name, ...)

A mock of a models Options object, based on the model signature.

MockModel(project_sig, app_name, model_name, ...)

A mock model.

MockRelated(related_model, model, field)

A mock RelatedObject for relation fields.

django_evolution.mock_models.create_field(project_sig, field_name, field_type, field_attrs, parent_model, related_model=None)

Create a Django field instance for the given signature data.

This creates a field in a way that’s compatible with a variety of versions of Django. It takes in data such as the field’s name and attributes and creates an instance that can be used like any field found on a model.

Parameters:
  • field_name (unicode) – The name of the field.

  • field_type (cls) – The class for the type of field being constructed. This must be a subclass of django.db.models.Field.

  • field_attrs (dict) – Attributes to set on the field.

  • parent_model (cls) – The parent model that would own this field. This must be a subclass of django.db.models.Model.

  • related_model (unicode, optional) – The full class path to a model this relates to. This requires a django.db.models.ForeignKey field type.

Returns:

A new field instance matching the provided data.

Return type:

django.db.models.Field

class django_evolution.mock_models.MockMeta(project_sig, app_name, model_name, model_sig, managed=False, auto_created=False)

Bases: object

A mock of a models Options object, based on the model signature.

This emulates the standard Meta class for a model, storing data and providing mock functions for setting up fields from a signature.

__init__(project_sig, app_name, model_name, model_sig, managed=False, auto_created=False)

Initialize the meta instance.

Parameters:
  • project_sig (django_evolution.signature.ProjectSignature) – The project’s schema signature.

  • app_name (unicode) – The name of the Django application owning the model.

  • model_name (unicode) – The name of the model.

  • model_sig (dict) – The model’s schema signature.

  • managed (bool, optional) – Whether this represents a model managed internally by Django, rather than a developer-created model.

  • auto_created (bool, optional) – Whether this represents an auto-created model (such as an intermediary many-to-many model).

property local_fields

A list of all local fields on the model.

property fields

A list of all local fields on the model.

property local_many_to_many

A list of all local Many-to-Many fields on the model.

property label

A label shown for this model.

New in version 2.2.

setup_fields(model, stub=False)

Set up the fields listed in the model’s signature.

For each field in the model signature’s list of fields, a field instance will be created and stored in _fields or _many_to_many (depending on the type of field).

Some fields (for instance, a field representing a primary key) may also influence the attributes on this model.

Parameters:
  • model (cls) – The model class owning this meta instance. This must be a subclass of django.db.models.Model.

  • stub (bool, optional) – If provided, only a primary key will be set up. This is used internally when creating relationships between models and fields in order to prevent recursive relationships.

__getattr__(name)

Return an attribute from the meta class.

This will look up the attribute from the correct location, depending on the attribute being accessed.

Parameters:

name (unicode) – The attribute name.

Returns:

The attribute value.

Return type:

object

get_field(name)

Return a field with the given name.

Parameters:

name (unicode) – The name of the field.

Returns:

The field with the given name.

Return type:

django.db.models.Field

Raises:

django.db.models.fields.FieldDoesNotExist – The field could not be found.

get_field_by_name(name)

Return information on a field with the given name.

This is a stub that provides only basic functionality. It will return information for a field with the given name, with most data hard-coded.

Parameters:

name (unicode) – The name of the field.

Returns:

A tuple of information for the following:

  • The field instance (django.db.models.Field)

  • The model (hard-coded as None)

  • Whether this field is owned by this model (hard-coded as True)

  • Whether this is for a many-to-many relationship (hard-coded as None)

Return type:

tuple

Raises:

django.db.models.fields.FieldDoesNotExist – The field could not be found.

class django_evolution.mock_models.MockModel(project_sig, app_name, model_name, model_sig, auto_created=False, managed=False, stub=False, db_name=None)

Bases: object

A mock model.

This replicates some of the state and functionality of a model for use when generating, reading, or mutating signatures.

__init__(project_sig, app_name, model_name, model_sig, auto_created=False, managed=False, stub=False, db_name=None)

Initialize the model.

Parameters:
  • project_sig (django_evolution.signature.ProjectSignature) – The project’s schema signature.

  • app_name (unicode) – The name of the Django app that owns the model.

  • model_name (unicode) – The name of the model.

  • model_sig (dict) – The model’s schema signature.

  • auto_created (bool, optional) – Whether this represents an auto-created model (such as an intermediary many-to-many model).

  • managed (bool, optional) – Whether this represents a model managed internally by Django, rather than a developer-created model.

  • stub (bool, optional) – Whether this is a stub model. This is used internally to create models that only contain a primary key field and no others, for use when dealing with circular relationships.

  • db_name (unicode, optional) – The name of the database where the model would be read from or written to.

__repr__()

Return a string representation of the model.

Returns:

A string representation of the model.

Return type:

unicode

__hash__()

Return a hash of the model instance.

This is used to allow the model instance to be used as a key in a dictionary.

Django would return a hash of the primary key’s value, but that’s not necessary for our needs, and we don’t have field values in most mock models.

Returns:

The hash of the model.

Return type:

int

__eq__(other)

Return whether two mock models are equal.

Both are considered equal if they’re both mock models with the same app name and model name.

Parameters:

other (MockModel) – The other mock model to compare to.

Returns:

True if both are equal. False if they are not.

Return type:

bool

class django_evolution.mock_models.MockRelated(related_model, model, field)

Bases: object

A mock RelatedObject for relation fields.

This replicates some of the state and functionality of django.db.models.related.RelatedObject, used for generating signatures and applying mutations.

__init__(related_model, model, field)

Initialize the object.

Parameters:
  • related_model (MockModel) – The mock model on the other end of the relation.

  • model (MockModel) – The mock model on this end of the relation.

  • field (django.db.models.Field) – The field owning the relation.