Skip to content

Instantly share code, notes, and snippets.

@PirosB3
Last active August 29, 2015 14:01
Show Gist options
  • Save PirosB3/d74503dd9ca45a287ebc to your computer and use it in GitHub Desktop.
Save PirosB3/d74503dd9ca45a287ebc to your computer and use it in GitHub Desktop.
API spec 2

Meta API Spec

INTERFACE

2 main API calls, get_fields and get_field.

>>> get_fields(types, opts, **kwargs)
(
  (field_name, field_instance),
  (field_name, field_instance),
  (field_name, field_instance),
)
>>> get_field(field_name, with_details=False)
field_instance

>>> get_field(field_name, with_details=True)
field_instance, model, is_direct, is_m2m

TYPES

DATA

Is referred to fields that actually contain information defined explicitly by the user. Example of this is DateField, CharField, BooleanField but this can also be a ForeignKey.

NOTE: do we want to have ForeignKey as part of DATA fields, or make it a separate type? My reasoning is to keep it as a data type until we realize there is a need of pulling it out.

By default it recursively searches through all the parent list and takes into consideration also non-concrete fields. This can be changed by using the options LOCAL_ONLY and CONCRETE

M2M

Is referred to ManyToManyField.

By default it recursively searches through all the parent list and excludes hidden fields. This can be changed by using the options INCLUDE_HIDDEN and LOCAL_ONLY

RELATED

Is referred to related objects and m2m fields.

By default it searches recursively through all related and m2m objects excluding hidden and proxy objects. The options ONLY_* are used to keep only a subset, the options INCLUDE_* are only used if OBJECTS are included and will not have any effect if ONLY_M2M. LOCAL_ONLY can be used on any of these options.

OPTS

Concrete

Only get fields where column is not None

LOCAL_ONLY

Only get fields on current model, do not recursively get fields of a type

INCLUDE_HIDDEN

Only possible on M2M fields (related and non): a hidden field is found through

is_hidden() == True

INCLUDE_PROXY

Proxy fields are found on a data field through:

field.model._meta == self

Proxy fields are found on a m2m field through:

field.rel.to._meta == self

ONLY_M2M

Limits related fields only to M2M

ONLY_OBJECTS

Limits related fields only to objects

Options Matrix

DATA M2M RELATED
CONCRETE YES YES NO
LOCAL_ONLY YES YES YES
INCLUDE_HIDDEN NO YES YES
INCLUDE_PROXY NO NO YES
ONLY_M2M NO NO YES
ONLY_OBJECTS NO NO YES
# AVAILABLE OPTIONS: CONCRETE, LOCAL_ONLY, INCLUDE_HIDDEN, INCLUDE_PROXY, ONLY_M2M, ONLY_OBJECTS
# AVAILABLE TYPES = DATA, M2M, RELATED
# def get_fields(types, opts):
# pass
@cached_property
def fields(self):
# get_fields(types=DATA)
def concrete_fields(self):
# get_fields(types=DATA, opts=CONCRETE)
def local_concrete_fields(self):
# get_fields(types=DATA, opts=CONCRETE | LOCAL_ONLY)
def get_fields_with_model(self):
# get_fields(types=DATA)
def get_concrete_fields_with_model(self):
# get_fields(types=DATA, opts=CONCRETE)
def _many_to_many(self):
# get_fields(types=M2M)
def get_m2m_with_model(self):
# get_fields(types=M2M)
def get_all_field_names(self):
# get_fields(types=DATA | M2M, opts=RELATED)
def get_all_related_many_to_many_objects(self, local_only=True:)
# get_fields(types=RELATED, opts=LOCAL_ONLY | ONLY_M2M) // if local_only=True
def get_all_related_m2m_objects_with_model(self):
# get_fields(types=RELATED, opts=ONLY_M2M)
def get_all_related_objects_with_model(self, local_only=False,
include_hidden=False,
include_proxy_eq=False):
# get_fields(types=RELATED, opts=ONLY_OBJECTS | LOCAL_ONLY | INCLUDE_HIDDEN | INCLUDE_PROXY)
@PirosB3
Copy link
Author

PirosB3 commented May 26, 2014

This API is always incrementing the query by each flag. It never decreases of excludes fields based on bits

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment