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 responsedata
from an APIendpoint
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 viaquery_endpoint()
- DOCUMENTATION: str = None
Link to the Official Magento 2 API documentation for the endpoint wrapped by the Model
- __init__(data, client, endpoint, private_keys=True)[source]View on GitHub
Initialize a
Model
object from an API response and theendpoint
that it came from…
Tip
The
endpoint
is used to:Generate the
url_for()
any requests made by subclass-specific methodsMatch the
Model
to its correspondingSearchQuery
objectDetermine how to
parse()
newModel
objects from API responses
…
- Parameters
- 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 theexcluded_keys
No matter what, the
status
attribute will not be set on theModel
If
private_keys==True
, the__status
attribute will be set (using thestatus
data)If
private_keys==False
, the data fromstatus
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
- query_endpoint()[source]View on GitHub
Initializes and returns the
SearchQuery
object corresponding to the Model’sendpoint
- Returns
a
SearchQuery
or subclass, depending on theendpoint
- Return type
- parse(response)[source]View on GitHub
Uses the instance’s corresponding
SearchQuery
to parse an API response
- 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 theClient.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
- 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'}
- 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'}]
- static encode(string)[source]View on GitHub
URL-encode with
urllib.parse
; used for requests that could contain special characters
- 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
…
- class magento.models.model.APIResponse(data, client, endpoint)[source]View on GitHub
Bases:
Model
- __init__(data, client, endpoint)[source]View on GitHub
A generic
Model
subclassWraps API responses when there isn’t a
Model
subclass defined for theendpoint
- 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: Optional[int]
Unique item identifier
Note
Since the
APIResponse
can wrap any endpoint, the response is checked for commonly used id fields (entity_id
andid
)If the endpoint doesn’t use those fields,
None
will be returned