本文整理汇总了Python中django.db.models.base.ModelBase类的典型用法代码示例。如果您正苦于以下问题:Python ModelBase类的具体用法?Python ModelBase怎么用?Python ModelBase使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ModelBase类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: add_to_class
def add_to_class(cls, name, value):
# Auto-specify a field's db_column with the table name on the front.
if isinstance(value, models.Field):
db_column_parts = [cls.__name__.lower(), value.db_column or name]
if isinstance(value, models.ForeignKey):
db_column_parts.append('id')
value.db_column = '_'.join(db_column_parts)
ModelBase.add_to_class(cls, name, value)
示例2: create_translations_model
def create_translations_model(model, related_name, meta, **fields):
"""
Create the translations model for the shared model 'model'.
'related_name' is the related name for the reverse FK from the translations
model.
'meta' is a (optional) dictionary of attributes for the translations model's
inner Meta class.
'fields' is a dictionary of fields to put on the translations model.
Two fields are enforced on the translations model:
language_code: A 15 char, db indexed field.
master: A ForeignKey back to the shared model.
Those two fields are unique together, this get's enforced in the inner Meta
class of the translations table
"""
if not meta:
meta = {}
unique = [('language_code', 'master')]
meta['unique_together'] = list(meta.get('unique_together', [])) + unique
# Create inner Meta class
Meta = type('Meta', (object,), meta)
if not hasattr(Meta, 'db_table'):
Meta.db_table = model._meta.db_table + '%stranslation' % getattr(settings, 'NANI_TABLE_NAME_SEPARATOR', '_')
Meta.app_label = model._meta.app_label
name = '%sTranslation' % model.__name__
attrs = {}
attrs.update(fields)
attrs['Meta'] = Meta
attrs['__module__'] = model.__module__
attrs['objects'] = TranslationsModelManager()
attrs['language_code'] = models.CharField(max_length=15, db_index=True)
# null=True is so we can prevent cascade deletion
attrs['master'] = models.ForeignKey(model, related_name=related_name,
editable=False, null=True)
# Create and return the new model
translations_model = ModelBase(name, (BaseTranslationModel,), attrs)
bases = (model.DoesNotExist, translations_model.DoesNotExist,)
DNE = type('DoesNotExist', bases, {})
translations_model.DoesNotExist = DNE
opts = translations_model._meta
opts.shared_model = model
# Register it as a global in the shared model's module.
# This is needed so that Translation model instances, and objects which
# refer to them, can be properly pickled and unpickled. The Django session
# and caching frameworks, in particular, depend on this behaviour.
mod = sys.modules[model.__module__]
setattr(mod, name, translations_model)
return translations_model
示例3: __init__
def __init__(cls, class_name, bases, attrs):
# Don't execute for Gallery itself
if [b for b in bases if isinstance(b, GalleryBase)]:
try:
gallery_meta = getattr(cls, 'GalleryMeta')
except AttributeError:
raise GalleryMetaNotDefined('%s must define GalleryMeta.' %
class_name)
try:
member_models = getattr(gallery_meta, 'member_models')
except AttributeError:
raise MemberModelsNotDefined(
'%s.GalleryMeta must define a list of member_models.' %
class_name)
gallery_meta.gallery_class = cls
cls._gallery_meta = gallery_meta
membership_verbose_name = '%s Membership' % cls._meta.verbose_name
custom_membership = getattr(gallery_meta, 'membership_class', None)
if custom_membership:
membership_class_name = '%sBaseMembership' % class_name
else:
membership_class_name = '%sMembership' % class_name
module_name = cls.__module__
membership_class = _create_membership_class(
class_name=membership_class_name,
verbose_name=membership_verbose_name,
app_label=cls._meta.app_label,
module_name=module_name,
member_models=member_models,
abstract=bool(custom_membership),
gallery_class=cls,
)
if custom_membership:
cls.BaseMembership = membership_class
module = importlib.import_module(module_name)
class Descriptor(object):
def __get__(self, instance, owner):
return getattr(module, custom_membership)
cls.Membership = Descriptor()
else:
cls.Membership = membership_class
ModelBase.__init__(cls, class_name, bases, attrs)
示例4: __new__
def __new__(cls, name, bases, attrs):
# redefine the related name in the foreign key to TournamentNode,
# so classes with the same name (from different apps) won't clash
try:
from tms.models import TournamentNode
except ImportError:
pass
else:
if (TournamentNode in bases and not
('Meta' in attrs and getattr(attrs['Meta'], 'proxy', False))):
attrs['tournamentnode_ptr'] = models.OneToOneField(
TournamentNode,
related_name="%(app_label)s_%(class)s_related",
parent_link=True
)
# add leaf class manager to all subclasses (unless they define their own)
if 'objects' not in attrs:
attrs['objects'] = ThisTypeOnlyManager()
NewNodeType = ModelBase.__new__(cls, name, bases, attrs)
type_id = NewNodeType.get_id()
def node_init(self, *args, **kwargs):
# TODO: preserve custom init method?
models.Model.__init__(self, *args, **kwargs)
if not self.type_id:
self.type_id = type_id
NewNodeType.__init__ = node_init
node_types[type_id] = NewNodeType
return NewNodeType
示例5: create_translations_model
def create_translations_model(self, model, related_name):
""" Create the translations model for a shared model.
model -- the model class to create translations for
related_name -- the related name for the reverse FK from the translations model.
"""
model_name = "%sTranslation" % model.__name__
translation_bases, translation_base_fields = self._scan_model_bases(model)
attrs = self.fields.copy()
attrs.update(
{
"Meta": self._build_meta_class(
model, translation_base_fields.union(self.fields).union(("language_code",))
),
"__module__": model.__module__,
}
)
if not model._meta.abstract:
attrs.update(
{
# If this class is abstract, we must not contribute management fields
"objects": TranslationsModelManager(),
"language_code": models.CharField(max_length=15, db_index=True),
# Nullable so we can prevent cascade deletion
"master": models.ForeignKey(
model, related_name=related_name, editable=False, null=True, on_delete=models.CASCADE
),
}
)
# Create the new model
if self.base_class:
translation_bases.insert(0, self.base_class)
translations_model = ModelBase(model_name, tuple(translation_bases), attrs)
translations_model._meta.shared_model = model
if not model._meta.abstract:
# Abstract models do not have a DNE class
bases = (model.DoesNotExist, translations_model.DoesNotExist)
translations_model.DoesNotExist = type("DoesNotExist", bases, {})
# Register it as a global in the shared model's module.
# This is needed so that Translation model instances, and objects which
# refer to them, can be properly pickled and unpickled. The Django session
# and caching frameworks, in particular, depend on this behaviour.
setattr(sys.modules[model.__module__], model_name, translations_model)
return translations_model
示例6: __new__
def __new__(cls, name, bases, attrs):
bases = list(bases)
new_class = ModelBase.__new__(cls, name, tuple(bases), attrs)
if new_class._meta.abstract:
pass
return new_class
示例7: __new__
def __new__(cls, name, bases, attrs):
# inject Profile fields
for k, v in cls.base_fields.items():
if k not in attrs:
attrs[k] = cls.base_fields[k]
model = ModelBase.__new__(cls, name, bases, attrs)
_profiles.append(model)
return model
示例8: __new__
def __new__(cls, name, bases, attrs):
# inject PageBase fields
for k, v in cls.base_fields.items():
if k not in attrs:
attrs[k] = cls.base_fields[k]
page_cls = ModelBase.__new__(cls, name, bases, attrs)
register(page_cls)
return page_cls
示例9: __new__
def __new__(cls, name, bases, attrs):
new_class = ModelBase.__new__(cls, name, bases, attrs)
if hasattr(new_class, 'FreeAdmin'):
new_class.add_to_class('_admin',
FreeAdminWrapper(new_class.FreeAdmin))
else:
new_class.add_to_class('_admin', FreeAdminWrapper())
return new_class
示例10: __new__
def __new__(cls, name, bases, attrs):
from freenasUI.freeadmin.site import site
new_class = ModelBase.__new__(cls, name, bases, attrs)
if new_class._meta.abstract:
pass
elif hasattr(new_class, 'FreeAdmin'):
site.register(new_class, freeadmin=new_class.FreeAdmin)
return new_class