The model module

class magento.models.model.Model(data, client, endpoint, private_keys=True)[source]View on GitHub

Bases: ABC

The abstract base class of all API response wrapper classes

Overview

  • A Model wraps the response data from an API endpoint

  • Several endpoints have subclasses with additional methods to retrieve/update data

  • All other endpoints are wrapped using a general APIResponse

  • The endpoint’s corresponding SearchQuery can be accessed via query_endpoint()

DOCUMENTATION: str = None

Link to the Official Magento 2 API documentation for the endpoint wrapped by the Model

IDENTIFIER: str = None

The API response field that the endpoint’s uid comes from

__init__(data, client, endpoint, private_keys=True)[source]View on GitHub

Initialize a Model object from an API response and the endpoint that it came from

Tip

The endpoint is used to:

  • Generate the url_for() any requests made by subclass-specific methods

  • Match the Model to its corresponding SearchQuery object

  • Determine how to parse() new Model objects from API responses

Parameters
  • data (dict) – the JSON from an API response to use as source data

  • client (Client) – an initialized Client

  • endpoint (str) – the API endpoint that the Model wraps

  • private_keys (bool) – if True, sets the keys in the excluded_keys as private attributes (prefixed with __) instead of fully excluding them

set_attrs(data, private_keys=True)[source]View on GitHub

Initializes object attributes using the JSON from an API response as the data source

Called at the time of object initialization, but can also be used to update the source data and reinitialize the attributes without creating a new object

Parameters
  • data (dict) – the API response JSON to use as the object source data

  • private_keys (bool) – if set to True, will set the excluded_keys as private attributes (prefixed with __) instead of fully excluding them

Private Keys Clarification

Let’s say that "status" is in the excluded_keys

  • No matter what, the status attribute will not be set on the Model

  • If private_keys==True, the __status attribute will be set (using the status data)

  • If private_keys==False, the data from status is completely excluded

abstract property excluded_keys: List[str]

API response keys that shouldn’t be set as object attributes by set_attrs()

Returns

list of API response keys that shouldn’t be set as attributes

property uid: Union[str, int]

Unique item identifier; used in the url of the data_endpoint()

data_endpoint(scope=None)[source]View on GitHub

Endpoint to use when requesting/updating the item’s data

Parameters

scope (Optional[str]) – the scope to generate the url_for()

Return type

str

query_endpoint()[source]View on GitHub

Initializes and returns the SearchQuery object corresponding to the Model’s endpoint

Returns

a SearchQuery or subclass, depending on the endpoint

Return type

SearchQuery

parse(response)[source]View on GitHub

Uses the instance’s corresponding SearchQuery to parse an API response

Parameters

response (dict) – API response dict to use as source data

Returns

a Model with the same endpoint as the calling instance

Return type

Model

refresh(scope=None)[source]View on GitHub

Updates object attributes in place using current data from the data_endpoint()

Hint

refresh() can be used to switch the scope of the source data without creating a new object or changing the Client.scope

Example

# Get product data on 'default' scope
>>> product = client.products.by_sku('sku42')
# Get fresh product data from different scope
>>> product.refresh('all')

Refreshed <Magento Product: sku42> on scope all
Parameters

scope (Optional[str]) – the scope to send the request on; uses the Client.scope if not provided

Return type

bool

static unpack_attributes(attributes, key='attribute_code')[source]View on GitHub

Unpacks a list of attribute dictionaries into a single dictionary

Example

>> custom_attrs = [{'attribute_code': 'attr', 'value': 'val'},{'attribute_code': 'will_to_live', 'value': '0'}]
>> print(Model.unpack_attributes(custom_attrs))

{'attr': 'val', 'will_to_live': '0'}
Parameters
  • attributes (List[dict]) – a list of custom attribute dictionaries

  • key (str) – the key used in the attribute dictionary (ex. attribute_code or label)

Returns

a single dictionary of all custom attributes formatted as {"attr": "val"}

Return type

dict

static pack_attributes(attribute_data, key='attribute_code')[source]View on GitHub

Packs a dictionary containing attributes into a list of attribute dictionaries

Example

>> attribute_data = {'special_price': 12, 'meta_title': 'My Product'}
>> print(Model.pack_attributes(attribute_data))
>> print(Model.pack_attributes(attribute_data, key='label'))

[{'attribute_code': 'special_price', 'value': 12}, {'attribute_code': 'meta_title', 'value': 'My Product'}]
[{'label': 'special_price', 'value': 12}, {'label': 'meta_title', 'value': 'My Product'}]
Parameters
  • attribute_data (dict) – a dictionary containing attribute data

  • key (str) – the key to use when packing the attributes (ex. attribute_code or label)

Returns

a list of dictionaries formatted as {key : "attr", "value": "value"}

Return type

List[dict]

static encode(string)[source]View on GitHub

URL-encode with urllib.parse; used for requests that could contain special characters

Parameters

string (str) – the string to URL-encode

Return type

str

property cached: List[str]

Names of properties that are wrapped with functools.cached_property()

clear(*keys)[source]View on GitHub

Deletes the provided keys from the object’s __dict__

To clear all cached properties:

>> self.clear(*self.cached)
Parameters

keys (str) – name of the object attribute(s) to delete

get_scope_name(scope)[source]View on GitHub

Returns the appropriate scope name to use for logging messages

Return type

str

class magento.models.model.APIResponse(data, client, endpoint)[source]View on GitHub

Bases: Model

IDENTIFIER: str = 'entity_id'

The API response field that the endpoint’s uid comes from

__init__(data, client, endpoint)[source]View on GitHub

A generic Model subclass

Wraps API responses when there isn’t a Model subclass defined for the endpoint

Parameters
  • data (dict) – the API response from an API endpoint

  • client (Client) – an initialized Client object

  • endpoint (str) – the endpoint that the API response came from

property excluded_keys: List[str]

API response keys that shouldn’t be set as object attributes by set_attrs()

Returns

list of API response keys that shouldn’t be set as attributes

data_endpoint(scope=None)[source]View on GitHub

Endpoint to use when requesting/updating the item’s data

Parameters

scope (Optional[str]) – the scope to generate the url_for()

Return type

Optional[str]

property uid: Optional[int]

Unique item identifier

Note

Since the APIResponse can wrap any endpoint, the response is checked for commonly used id fields (entity_id and id)

If the endpoint doesn’t use those fields, None will be returned