Entry Variants
Entry Variants
Entry variants are alternative versions of an entry for different audiences. Use this class to
- read one entry with the content of one or more variants
- list the entries of a content type with the content of one or more variants
- send either request to a specific branch
Two factory methods, entry.variants() and content_type.variants(), return a Variants object, and both take the same arguments:
| Name | Type | Description |
|---|---|---|
| variant_uid | str | list[str] | Variant UID, or a list of them, to apply. |
| branch | str | Branch to send the variant request to. Optional. |
| params | dict | Query parameters to send with the variant request. Optional. |
Instance State
The factory you call decides whether fetch works.
- Call entry.variants('<VARIANT_UID>') on an Entry to read that entry. The object stores the entry UID, so fetch works.
- Call content_type.variants('<VARIANT_UID>') on a content type to list its entries. The object stores no entry UID, so find works and fetch raises ValueError.
- find works on both objects and always lists the entries of the content type. It ignores the entry UID.
Both factories return the same class, so nothing distinguishes the two objects until fetch raises.
Warning content_type.entry('') does not raise, and fetch checks the entry UID against None only. An empty string therefore builds a request URL that ends in /entries/ with no UID. Check that a UID variable is non-empty before you pass it.
Class-Level Notes
Variant UIDs travel in a request header.
- fetch and find send the variant UIDs in the x-cs-variant-uid header, not in the URL.
- The SDK sends a str as given and joins a list with commas. By default the API accepts up to 5 variant UIDs or aliases, and the first one takes priority on the base entry fields. See the Get single entry variant request.
Known issue A variant_uid of any type other than str or list, such as a tuple or None, sends no variant header. The request then returns base content with no error. Convert a tuple to a list before you pass it.
The SDK changes the shared Stack headers during each request.
- The SDK writes x-cs-variant-uid, and branch when you pass one, into the headers that every object from the same Stack shares. It removes them when the response arrives, and puts back the earlier branch value.
Known issue When a variant request fails with a network error, the SDK skips that cleanup, so every later request from the same Stack keeps the variant header and returns variant content. In the except block, call stack.get_headers.pop('x-cs-variant-uid', None), and reset the branch header if you passed one.
Parameters go through params only.
- fetch and find send the keys from the params argument of the factory and of each call. The object keeps the dictionary you pass, not a copy, and adds each call's keys to it, so the keys stay for later calls.
- The SDK encodes a list value as its Python text rather than as repeated keys. Pass one string per key.
- Variant requests send the environment in the environment request header only. They add no environment query parameter.
Known issue The nine methods Variants inherits from EntryQueryable, such as locale and include_metadata, have no effect. The variant request never reads the values they store. Pass the same keys in params instead, for example fetch({'locale': 'fr-fr'}).
Rate limiting and retry.
- This SDK retries 408 and 429 responses automatically. The default retry_strategy sends the initial request plus up to five retries, with no delay between them, because its backoff_factor is 0.
- Pass your own retry_strategy to Stack with a non-zero backoff factor if you expect to hit 429.
Additional Resource Implement Retry Mechanism with Python Delivery SDK covers the retry_strategy parameters and backoff configuration.
Capability Matrix
| Capability | Supported | Notes / Alternative |
|---|---|---|
| Chain locale, only, or another inherited method | No | Pass the same keys in params. |
| A list value in params | No | Pass one string per key. |
| List the variants of one entry with find | No | find lists every entry of the content type. Use fetch for one entry. |
| Carry parameters chained on the Entry into the variant request | No | Pass them in the params argument of variants(). |
Method Index
The groups below follow the Instance State split. fetch needs an object from entry.variants(), and every other method works on both objects.
Read variant content. fetch is available on entry.variants(), and find on both factories.
| Method Name | Returns | Description |
|---|---|---|
| fetch | The requested entry with its variant content. | Reads one entry with variant content. |
| find | The matching entries with their variant content. | Lists the entries of the content type with variant content. |
Inherited methods with no effect. Available on both factories. Each stores a value that the variant request does not send.
| Method Name | Returns | Description |
|---|---|---|
| locale | The same variants object, for chaining. | Stores a locale code. |
| only | The same variants object, for chaining. | Stores one field UID to keep. |
| excepts | The same variants object, for chaining. | Stores one field UID to leave out. |
| include_reference | The same variants object, for chaining. | Stores reference fields to resolve. |
| include_content_type | The same variants object, for chaining. | Stores two content type flags. |
| include_reference_content_type_uid | The same variants object, for chaining. | Stores a reference content type UID flag. |
| include_metadata | The same variants object, for chaining. | Stores the metadata flag. |
| asset_fields | The same variants object, for chaining. | Stores asset field group names. |
| add_param | The same variants object, for chaining. | Stores one query parameter. |
Class-Level Snippet
This snippet reads one entry in French with two variants on a branch, and removes the variant header if the request fails.
import contentstack
stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>')
variants = stack.content_type('<CONTENT_TYPE_UID>') \
.entry('<ENTRY_UID>') \
.variants(['<VARIANT_UID>', '<SECOND_VARIANT_UID>'], branch='development', params={'locale': 'fr-fr'})
try:
result = variants.fetch()
except Exception as error:
# Raised only for network failures (timeout, DNS error, dropped connection)
print('Request failed:', error)
# The SDK skips its header cleanup on a network failure.
stack.get_headers.pop('x-cs-variant-uid', None)
# This Stack has no branch of its own, so remove the one the request added.
stack.get_headers.pop('branch', None)
else:
if 'error_code' in result:
# The Delivery API rejected the request and returned the error body
print('Delivery API error:', result['error_code'], result['error_message'])
else:
print(result['entry']['title'])Methods
Each method of the Variants class has its own section below, with its parameters, validation rules, behavior, and examples.
add_param
add_param stores one query parameter. The variant request does not send it.
| Name | Type | Description |
|---|---|---|
| key (required) | str | Query parameter name. |
| value (required) | str | Value for that parameter. |
Returns: Variants. The same variants object, for chaining.
Validation
- There is no client-side validation. When key or value is None, add_param records nothing and raises nothing.
- Calling add_param() with fewer than two arguments raises TypeError, because add_param takes key and value as positional parameters with no default.
- Errors that do occur come from the fetch() or find() call that follows, not from add_param().
- fetch() and find() raise an exception only for network failures (timeout, DNS error, dropped connection).
- An API-level rejection returns as a normal result with an error_code key. It does not raise an exception.
- Check the result for an error_code key even inside a try block.
Additional Resource Refer to Delivery API Errors for the full error code list.
Behavior
- Variants inherits add_param from EntryQueryable. add_param writes a parameter store that the fetch and find methods never read, so the value has no effect on the variant request.
- Pass the same parameter in the params argument of fetch or find instead, for example fetch({'include_publish_details': 'true'}).
- See the inherited methods note on the Variants class page.
Example
Workaround: send the value through params
import contentstack
stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>')
variants = stack.content_type('<CONTENT_TYPE_UID>').entry('<ENTRY_UID>').variants('<VARIANT_UID>')
# add_param() records the value, but fetch() does not send it.
variants.add_param('include_publish_details', 'true')
try:
# params carries the value to the API.
result = variants.fetch({'include_publish_details': 'true'})
except Exception as error:
# Raised only for network failures (timeout, DNS error, dropped connection)
print('Request failed:', error)
else:
if 'error_code' in result:
# The Delivery API rejected the request and returned the error body
print('Delivery API error:', result['error_code'], result['error_message'])
else:
print(result['entry']['title'])asset_fields
asset_fields stores asset field group names. The variant request does not send them.
| Name | Type | Description |
|---|---|---|
| field_names (required) | str | list[str] | Asset field group names. Pass them as separate arguments. |
Returns: Variants. The same variants object, for chaining.
Validation
- There is no client-side validation. asset_fields converts each value with str(), and a call with no arguments changes nothing.
- Errors that do occur come from the fetch() or find() call that follows, not from asset_fields().
- fetch() and find() raise an exception only for network failures (timeout, DNS error, dropped connection).
- An API-level rejection returns as a normal result with an error_code key. It does not raise an exception.
- Check the result for an error_code key even inside a try block.
Additional Resource Refer to Delivery API Errors for the full error code list.
Behavior
- Variants inherits asset_fields from EntryQueryable. asset_fields writes a parameter store that the fetch and find methods never read, so the value has no effect on the variant request.
- Pass the same parameter in the params argument of fetch or find instead, for example fetch({'asset_fields[]': 'visual_markups'}).
- See the inherited methods note on the Variants class page.
Example
Workaround: send the value through params
import contentstack
stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>')
variants = stack.content_type('<CONTENT_TYPE_UID>').entry('<ENTRY_UID>').variants('<VARIANT_UID>')
# asset_fields() records the value, but fetch() does not send it.
variants.asset_fields('visual_markups')
try:
# params carries the value to the API.
result = variants.fetch({'asset_fields[]': 'visual_markups'})
except Exception as error:
# Raised only for network failures (timeout, DNS error, dropped connection)
print('Request failed:', error)
else:
if 'error_code' in result:
# The Delivery API rejected the request and returned the error body
print('Delivery API error:', result['error_code'], result['error_message'])
else:
print(result['entry']['title'])excepts
excepts stores one field UID to leave out. The variant request does not send it.
| Name | Type | Description |
|---|---|---|
| field_uid (required) | str | UID of the top-level field to leave out. |
Returns: Variants. The same variants object, for chaining.
Validation
- excepts raises KeyError if field_uid is a non-null value that is not a string, such as a list, because it stores a single string.
- A None value records nothing and raises nothing.
- Calling excepts() without an argument raises TypeError, because field_uid is a required positional parameter.
- Errors that do occur come from the fetch() or find() call that follows, not from excepts().
- fetch() and find() raise an exception only for network failures (timeout, DNS error, dropped connection).
- An API-level rejection returns as a normal result with an error_code key. It does not raise an exception.
- Check the result for an error_code key even inside a try block.
Additional Resource Refer to Delivery API Errors for the full error code list.
Behavior
- Variants inherits excepts from EntryQueryable. excepts writes a parameter store that the fetch and find methods never read, so the value has no effect on the variant request.
- Pass the same parameter in the params argument of fetch or find instead, for example fetch({'except[BASE][]': 'body'}).
- See the inherited methods note on the Variants class page.
Example
Workaround: send the value through params
import contentstack
stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>')
variants = stack.content_type('<CONTENT_TYPE_UID>').entry('<ENTRY_UID>').variants('<VARIANT_UID>')
# excepts() records the value, but fetch() does not send it.
variants.excepts('body')
try:
# params carries the value to the API.
result = variants.fetch({'except[BASE][]': 'body'})
except Exception as error:
# Raised only for network failures (timeout, DNS error, dropped connection)
print('Request failed:', error)
else:
if 'error_code' in result:
# The Delivery API rejected the request and returned the error body
print('Delivery API error:', result['error_code'], result['error_message'])
else:
print(result['entry']['title'])fetch
fetch reads one entry with the content of the variants you named and returns the response body.
| Name | Type | Description |
|---|---|---|
| params | dict | Query parameters to add for this request. |
Returns: dict. The requested entry with its variant content.
Validation
- fetch raises ValueError if the object holds no entry UID, because it reads one entry by UID. An object from content_type.variants() never holds one. Use the find method on that object.
- An empty entry UID passes that check. The request URL then ends in /entries/ with no UID.
- fetch raises TypeError if variant_uid is a list that holds a non-string item, because the SDK joins the list with commas.
- A params value that dict.update cannot merge, such as a string, raises ValueError or TypeError.
- fetch raises an exception only for network failures (timeout, DNS error, dropped connection).
- An API-level rejection returns as a normal result with an error_code key. It does not raise an exception.
- Check the result for an error_code key even inside a try block.
- A network failure raises before the SDK removes the variant header, so later requests from the same Stack still send it. In the except block, call stack.get_headers.pop('x-cs-variant-uid', None), and reset the branch header if you passed one. See the Known issue on the Variants class page.
Additional Resource Refer to Delivery API Errors for the full error code list.
Behavior
- Each call sends one GET /content_types/{content_type_uid}/entries/{entry_uid} request with the x-cs-variant-uid header. See the Get single entry variant request.
- fetch returns the whole response body as a dictionary. Read the entry from its entry key.
- fetch merges params into the dictionary the object already holds, then sends every key in it.
- The keys stay on the object, so a later call on the same object sends them again.
- The SDK encodes a list value as its Python text, such as ['a', 'b'], not as repeated keys. Pass one string per key.
- When the object holds a branch, fetch sends it as the branch header for this request and then puts the previous value back.
Example
Basic usage: read an entry with one variant
import contentstack
stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>')
try:
result = stack.content_type('<CONTENT_TYPE_UID>').entry('<ENTRY_UID>').variants('<VARIANT_UID>').fetch()
except Exception as error:
# Raised only for network failures (timeout, DNS error, dropped connection)
print('Request failed:', error)
else:
if 'error_code' in result:
# The Delivery API rejected the request and returned the error body
print('Delivery API error:', result['error_code'], result['error_message'])
else:
print(result['entry']['title'])All parameters: read the French version with metadata
import contentstack
stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>')
try:
variants = stack.content_type('<CONTENT_TYPE_UID>').entry('<ENTRY_UID>').variants(['<VARIANT_UID>', '<SECOND_VARIANT_UID>'])
result = variants.fetch({'locale': 'fr-fr', 'include_metadata': 'true'})
print(result.get('entry'))
except Exception as error:
print('Request failed:', error)Edge case: an object without an entry UID
import contentstack
stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>')
try:
stack.content_type('<CONTENT_TYPE_UID>').variants('<VARIANT_UID>').fetch()
except ValueError as error:
# Fires because content_type.variants() holds no entry UID. Use find() instead.
print('No entry UID:', error)find
find lists the entries of the content type with the content of the variants you named and returns the response body.
| Name | Type | Description |
|---|---|---|
| params | dict | Query parameters to add for this request. |
Returns: dict. The matching entries with their variant content.
Validation
- There is no client-side validation of the content type UID. An object from stack.content_type() with no argument sends None as the content type UID in the path.
- find raises TypeError if variant_uid is a list that holds a non-string item, because the SDK joins the list with commas.
- A params value that dict.update cannot merge, such as a string, raises ValueError or TypeError.
- find raises an exception only for network failures (timeout, DNS error, dropped connection).
- An API-level rejection returns as a normal result with an error_code key. It does not raise an exception.
- Check the result for an error_code key even inside a try block.
- A network failure raises before the SDK removes the variant header, so later requests from the same Stack still send it. In the except block, call stack.get_headers.pop('x-cs-variant-uid', None), and reset the branch header if you passed one. See the Known issue on the Variants class page.
Additional Resource Refer to Delivery API Errors for the full error code list.
Behavior
- Each call sends one GET /content_types/{content_type_uid}/entries request with the x-cs-variant-uid header. See the Get multiple variants of an entry request.
- find reads the content type UID only. On an object from entry.variants(), find ignores the entry UID and lists every entry of the content type.
- find returns the whole response body as a dictionary. Read the entries from its entries key.
- find returns one page. Pass limit and skip in params to read later pages.
- find merges params into the dictionary the object already holds, then sends every key in it.
- The keys stay on the object, so a later call on the same object sends them again.
- The SDK encodes a list value as its Python text, such as ['a', 'b'], not as repeated keys. Pass one string per key.
- When the object holds a branch, find sends it as the branch header for this request and then puts the previous value back.
Example
Basic usage: list entries with one variant
import contentstack
stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>')
try:
result = stack.content_type('<CONTENT_TYPE_UID>').variants('<VARIANT_UID>').find()
except Exception as error:
# Raised only for network failures (timeout, DNS error, dropped connection)
print('Request failed:', error)
else:
if 'error_code' in result:
# The Delivery API rejected the request and returned the error body
print('Delivery API error:', result['error_code'], result['error_message'])
else:
for entry in result['entries']:
print(entry['title'])All parameters: a French page of ten entries on a branch
import contentstack
stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>')
try:
variants = stack.content_type('<CONTENT_TYPE_UID>').variants('<VARIANT_UID>', branch='development')
result = variants.find({'locale': 'fr-fr', 'limit': '10', 'skip': '0'})
print(result.get('entries'))
except Exception as error:
print('Request failed:', error)include_content_type
include_content_type stores two content type flags. The variant request does not send them.
This method takes no parameters.
Returns: Variants. The same variants object, for chaining.
Validation
- There is no client-side validation. include_content_type cannot fail.
- Errors that do occur come from the fetch() or find() call that follows, not from include_content_type().
- fetch() and find() raise an exception only for network failures (timeout, DNS error, dropped connection).
- An API-level rejection returns as a normal result with an error_code key. It does not raise an exception.
- Check the result for an error_code key even inside a try block.
Additional Resource Refer to Delivery API Errors for the full error code list.
Behavior
- Variants inherits include_content_type from EntryQueryable. include_content_type writes a parameter store that the fetch and find methods never read, so the value has no effect on the variant request.
- Pass the same parameter in the params argument of fetch or find instead, for example fetch({'include_content_type': 'true', 'include_global_field_schema': 'true'}).
- See the inherited methods note on the Variants class page.
Example
Workaround: send the value through params
import contentstack
stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>')
variants = stack.content_type('<CONTENT_TYPE_UID>').entry('<ENTRY_UID>').variants('<VARIANT_UID>')
# include_content_type() records the value, but fetch() does not send it.
variants.include_content_type()
try:
# params carries the value to the API.
result = variants.fetch({'include_content_type': 'true', 'include_global_field_schema': 'true'})
except Exception as error:
# Raised only for network failures (timeout, DNS error, dropped connection)
print('Request failed:', error)
else:
if 'error_code' in result:
# The Delivery API rejected the request and returned the error body
print('Delivery API error:', result['error_code'], result['error_message'])
else:
print(result['entry']['title'])include_metadata
include_metadata stores the metadata flag. The variant request does not send it.
This method takes no parameters.
Returns: Variants. The same variants object, for chaining.
Validation
- There is no client-side validation. include_metadata cannot fail.
- Errors that do occur come from the fetch() or find() call that follows, not from include_metadata().
- fetch() and find() raise an exception only for network failures (timeout, DNS error, dropped connection).
- An API-level rejection returns as a normal result with an error_code key. It does not raise an exception.
- Check the result for an error_code key even inside a try block.
Additional Resource Refer to Delivery API Errors for the full error code list.
Behavior
- Variants inherits include_metadata from EntryQueryable. include_metadata writes a parameter store that the fetch and find methods never read, so the value has no effect on the variant request.
- Pass the same parameter in the params argument of fetch or find instead, for example fetch({'include_metadata': 'true'}).
- The variant requests accept include_metadata as a query parameter. See the Get single entry variant request.
- See the inherited methods note on the Variants class page.
Example
Workaround: send the value through params
import contentstack
stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>')
variants = stack.content_type('<CONTENT_TYPE_UID>').entry('<ENTRY_UID>').variants('<VARIANT_UID>')
# include_metadata() records the value, but fetch() does not send it.
variants.include_metadata()
try:
# params carries the value to the API.
result = variants.fetch({'include_metadata': 'true'})
except Exception as error:
# Raised only for network failures (timeout, DNS error, dropped connection)
print('Request failed:', error)
else:
if 'error_code' in result:
# The Delivery API rejected the request and returned the error body
print('Delivery API error:', result['error_code'], result['error_message'])
else:
print(result['entry']['title'])include_reference
include_reference stores the reference fields to resolve. The variant request does not send them.
| Name | Type | Description |
|---|---|---|
| field_uid (required) | str | list[str] | Reference field UID, or a list of them. |
Returns: Variants. The same variants object, for chaining.
Validation
- There is no client-side validation. A value that is not a str or a list records nothing and raises nothing.
- Calling include_reference() without an argument raises TypeError, because field_uid is a required positional parameter.
- Errors that do occur come from the fetch() or find() call that follows, not from include_reference().
- fetch() and find() raise an exception only for network failures (timeout, DNS error, dropped connection).
- An API-level rejection returns as a normal result with an error_code key. It does not raise an exception.
- Check the result for an error_code key even inside a try block.
Additional Resource Refer to Delivery API Errors for the full error code list.
Behavior
- Variants inherits include_reference from EntryQueryable. include_reference writes a parameter store that the fetch and find methods never read, so the value has no effect on the variant request.
- Pass the same parameter in the params argument of fetch or find instead, for example fetch({'include[]': 'author'}).
- See the inherited methods note on the Variants class page.
Example
Workaround: send the value through params
import contentstack
stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>')
variants = stack.content_type('<CONTENT_TYPE_UID>').entry('<ENTRY_UID>').variants('<VARIANT_UID>')
# include_reference() records the value, but fetch() does not send it.
variants.include_reference('author')
try:
# params carries the value to the API.
result = variants.fetch({'include[]': 'author'})
except Exception as error:
# Raised only for network failures (timeout, DNS error, dropped connection)
print('Request failed:', error)
else:
if 'error_code' in result:
# The Delivery API rejected the request and returned the error body
print('Delivery API error:', result['error_code'], result['error_message'])
else:
print(result['entry']['title'])include_reference_content_type_uid
include_reference_content_type_uid stores a flag for the content type UID of referenced entries. The variant request does not send it.
This method takes no parameters.
Returns: Variants. The same variants object, for chaining.
Validation
- There is no client-side validation. include_reference_content_type_uid cannot fail.
- Errors that do occur come from the fetch() or find() call that follows, not from include_reference_content_type_uid().
- fetch() and find() raise an exception only for network failures (timeout, DNS error, dropped connection).
- An API-level rejection returns as a normal result with an error_code key. It does not raise an exception.
- Check the result for an error_code key even inside a try block.
Additional Resource Refer to Delivery API Errors for the full error code list.
Behavior
- Variants inherits include_reference_content_type_uid from EntryQueryable. include_reference_content_type_uid writes a parameter store that the fetch and find methods never read, so the value has no effect on the variant request.
- Pass the same parameter in the params argument of fetch or find instead, for example fetch({'include_reference_content_type_uid': 'true'}).
- See the inherited methods note on the Variants class page.
Example
Workaround: send the value through params
import contentstack
stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>')
variants = stack.content_type('<CONTENT_TYPE_UID>').entry('<ENTRY_UID>').variants('<VARIANT_UID>')
# include_reference_content_type_uid() records the value, but fetch() does not send it.
variants.include_reference_content_type_uid()
try:
# params carries the value to the API.
result = variants.fetch({'include_reference_content_type_uid': 'true'})
except Exception as error:
# Raised only for network failures (timeout, DNS error, dropped connection)
print('Request failed:', error)
else:
if 'error_code' in result:
# The Delivery API rejected the request and returned the error body
print('Delivery API error:', result['error_code'], result['error_message'])
else:
print(result['entry']['title'])locale
locale stores a locale code on the variants object. The variant request does not send it.
| Name | Type | Description |
|---|---|---|
| locale (required) | str | Locale code, for example fr-fr. |
Returns: Variants. The same variants object, for chaining.
Validation
- There is no client-side validation. locale keeps any value as given.
- Calling locale() without an argument raises TypeError, because locale is a required positional parameter.
- Errors that do occur come from the fetch() or find() call that follows, not from locale().
- fetch() and find() raise an exception only for network failures (timeout, DNS error, dropped connection).
- An API-level rejection returns as a normal result with an error_code key. It does not raise an exception.
- Check the result for an error_code key even inside a try block.
Additional Resource Refer to Delivery API Errors for the full error code list.
Behavior
- Variants inherits locale from EntryQueryable. locale writes a parameter store that the fetch and find methods never read, so the value has no effect on the variant request.
- Pass the same parameter in the params argument of fetch or find instead, for example fetch({'locale': 'fr-fr'}).
- The variant requests accept locale as a query parameter. See the Get single entry variant request.
- See the inherited methods note on the Variants class page.
Example
Workaround: send the value through params
import contentstack
stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>')
variants = stack.content_type('<CONTENT_TYPE_UID>').entry('<ENTRY_UID>').variants('<VARIANT_UID>')
# locale() records the value, but fetch() does not send it.
variants.locale('fr-fr')
try:
# params carries the value to the API.
result = variants.fetch({'locale': 'fr-fr'})
except Exception as error:
# Raised only for network failures (timeout, DNS error, dropped connection)
print('Request failed:', error)
else:
if 'error_code' in result:
# The Delivery API rejected the request and returned the error body
print('Delivery API error:', result['error_code'], result['error_message'])
else:
print(result['entry']['title'])only
only stores one field UID to keep. The variant request does not send it.
| Name | Type | Description |
|---|---|---|
| field_uid (required) | str | UID of the top-level field to keep. |
Returns: Variants. The same variants object, for chaining.
Validation
- only raises KeyError if field_uid is a non-null value that is not a string, such as a list, because it stores a single string.
- A None value records nothing and raises nothing.
- Calling only() without an argument raises TypeError, because field_uid is a required positional parameter.
- Errors that do occur come from the fetch() or find() call that follows, not from only().
- fetch() and find() raise an exception only for network failures (timeout, DNS error, dropped connection).
- An API-level rejection returns as a normal result with an error_code key. It does not raise an exception.
- Check the result for an error_code key even inside a try block.
Additional Resource Refer to Delivery API Errors for the full error code list.
Behavior
- Variants inherits only from EntryQueryable. only writes a parameter store that the fetch and find methods never read, so the value has no effect on the variant request.
- Pass the same parameter in the params argument of fetch or find instead, for example fetch({'only[BASE][]': 'title'}).
- See the inherited methods note on the Variants class page.
Example
Workaround: send the value through params
import contentstack
stack = contentstack.Stack('<API_KEY>', '<DELIVERY_TOKEN>', '<ENVIRONMENT>')
variants = stack.content_type('<CONTENT_TYPE_UID>').entry('<ENTRY_UID>').variants('<VARIANT_UID>')
# only() records the value, but fetch() does not send it.
variants.only('title')
try:
# params carries the value to the API.
result = variants.fetch({'only[BASE][]': 'title'})
except Exception as error:
# Raised only for network failures (timeout, DNS error, dropped connection)
print('Request failed:', error)
else:
if 'error_code' in result:
# The Delivery API rejected the request and returned the error body
print('Delivery API error:', result['error_code'], result['error_message'])
else:
print(result['entry']['title'])