field – Contains Special Field Types¶
Contains the special field types for PyFactory.
-
pyfactory.field.association(*args, **kwargs)[source]¶ Shortcut method for enabling an association field. See documentation of
AssociationField.
-
pyfactory.field.sequence(*args, **kwargs)[source]¶ Shortcut method for enabling a sequence field. See documentation of
SequenceField.
-
class
pyfactory.field.Field[source]¶ If you wish to implement a custom field which has special behavior, you must inherit and implement the methods in this class. Fields are how things such as
association()andsequence()are implemented.-
resolve(scope)[source]¶ This method is called by PyFactory to resolve the value of a special field. The
scopeargument will be one ofattributes,build, orcreatedepending on what method was called on the factory.The return value of this method is what is put into the actual attributes dict for the model.
-
-
class
pyfactory.field.AssociationField(factory, schema, attr=None)[source]¶ This marks the field value as the result of creating another model from another factory.
If
attris specified, then that attribute value will be the value of the association. This requires that the model builder understand thegetattrclass method, otherwise an exception will be raised.- Parameters
factory: An instance of a
Factoryto get the model from.schema: The name of the schema to load.
- attr (optional): The name of the attribute to read from the
resulting model to place as the value of this field. If not given, the entire model becomes the value of the field.
-
attribute(attr)[source]¶ - Parameters
attr: The name of the attribute to resolve to.
This will return a new
Fieldinstance which resolves to the value of the givenattrof this association. This method is useful to re-use a single association multiple times for different values. For example, say you have a schema which uses theidand thenamefield of a user. You could then define the schema like so:@schema() def example(self): user = association(UserFactory(), "basic") return { "remote_id": user.attribute("id"), "remote_name": user.attribute("name") }
The benefit of the above, instead of using two separate
associations, is that the association in this case will resolve to the exact same model, whereas twoassociationswill always resolve to two different models.
-
callback(callback)[source]¶ - Parameters
callback: Callback to be called, with the association as a parameter.
This will return a new
Fieldinstance which when resolved will call the given callback, passing the association as a parameter. To read an attribute from the association, use the dictionary syntax ofassociation[key]. This will do the right thing depending on whether you’re resolving attributes or a model build.As an example, let’s say you want to create a schema which depends on the concatenated first and last name of a user. Here is an example:
@schema() def example(self): def get_name(association): return "%s %s" % \ (association["first_name"], association["last_name"]) user = association(UserFactory(), "basic") return { "name": user.callback(get_name) }
-
class
pyfactory.field.SequenceField(string, interpolation_variable='n', unique_key='_default')[source]¶ This causes the value of a field to be interpolated with a sequential value. The
interpolation_variablewill be the variable name used for interpolation in the string.- Parameters
string: The string to perform the sequence interpolation on.
interpolation_variable (optional): The name of the variable to use for interpolation in the
string.unique_key (optional): The unique key to base the sequence creation on. By default, every time you call sequence, no matter what model factory or schema you’re in, the sequence will increase. By specifying a unique
unique_key, it isolates the increment to that key.