本文整理汇总了Python中modelcluster.fields.ParentalKey方法的典型用法代码示例。如果您正苦于以下问题:Python fields.ParentalKey方法的具体用法?Python fields.ParentalKey怎么用?Python fields.ParentalKey使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类modelcluster.fields
的用法示例。
在下文中一共展示了fields.ParentalKey方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_parental_key_checks_clusterable_model
# 需要导入模块: from modelcluster import fields [as 别名]
# 或者: from modelcluster.fields import ParentalKey [as 别名]
def test_parental_key_checks_clusterable_model(self):
from django.core import checks
from django.db import models
from modelcluster.fields import ParentalKey
class Instrument(models.Model):
# Oops, BandMember is not a Clusterable model
member = ParentalKey(BandMember, on_delete=models.CASCADE)
class Meta:
# Prevent Django from thinking this is in the database
# This shouldn't affect the test
abstract = True
# Check for error
errors = Instrument.check()
self.assertEqual(1, len(errors))
# Check the error itself
error = errors[0]
self.assertIsInstance(error, checks.Error)
self.assertEqual(error.id, 'modelcluster.E001')
self.assertEqual(error.obj, Instrument.member.field)
self.assertEqual(error.msg, 'ParentalKey must point to a subclass of ClusterableModel.')
self.assertEqual(error.hint, 'Change tests.BandMember into a ClusterableModel or use a ForeignKey instead.')
示例2: test_parental_key_checks_related_name_is_not_plus
# 需要导入模块: from modelcluster import fields [as 别名]
# 或者: from modelcluster.fields import ParentalKey [as 别名]
def test_parental_key_checks_related_name_is_not_plus(self):
from django.core import checks
from django.db import models
from modelcluster.fields import ParentalKey
class Instrument(models.Model):
# Oops, related_name='+' is not allowed
band = ParentalKey(Band, related_name='+', on_delete=models.CASCADE)
class Meta:
# Prevent Django from thinking this is in the database
# This shouldn't affect the test
abstract = True
# Check for error
errors = Instrument.check()
self.assertEqual(1, len(errors))
# Check the error itself
error = errors[0]
self.assertIsInstance(error, checks.Error)
self.assertEqual(error.id, 'modelcluster.E002')
self.assertEqual(error.obj, Instrument.band.field)
self.assertEqual(error.msg, "related_name='+' is not allowed on ParentalKey fields")
self.assertEqual(error.hint, "Either change it to a valid name or remove it")
示例3: test_parental_key_checks_target_is_resolved_as_class
# 需要导入模块: from modelcluster import fields [as 别名]
# 或者: from modelcluster.fields import ParentalKey [as 别名]
def test_parental_key_checks_target_is_resolved_as_class(self):
from django.core import checks
from django.db import models
from modelcluster.fields import ParentalKey
class Instrument(models.Model):
banana = ParentalKey('Banana', on_delete=models.CASCADE)
class Meta:
# Prevent Django from thinking this is in the database
# This shouldn't affect the test
abstract = True
# Check for error
errors = Instrument.check()
self.assertEqual(1, len(errors))
# Check the error itself
error = errors[0]
self.assertIsInstance(error, checks.Error)
self.assertEqual(error.id, 'fields.E300')
self.assertEqual(error.obj, Instrument.banana.field)
self.assertEqual(error.msg, "Field defines a relation with model 'Banana', which is either not installed, or is abstract.")
示例4: get_all_child_relations
# 需要导入模块: from modelcluster import fields [as 别名]
# 或者: from modelcluster.fields import ParentalKey [as 别名]
def get_all_child_relations(model):
"""
Return a list of RelatedObject records for child relations of the given model,
including ones attached to ancestors of the model
"""
return [
field for field in model._meta.get_fields()
if isinstance(field.remote_field, ParentalKey)
]
示例5: get_object_usage
# 需要导入模块: from modelcluster import fields [as 别名]
# 或者: from modelcluster.fields import ParentalKey [as 别名]
def get_object_usage(obj):
"Returns a queryset of pages that link to a particular object"
pages = Page.objects.none()
# get all the relation objects for obj
relations = [f for f in type(obj)._meta.get_fields(include_hidden=True)
if (f.one_to_many or f.one_to_one) and f.auto_created]
for relation in relations:
related_model = relation.related_model
# if the relation is between obj and a page, get the page
if issubclass(related_model, Page):
pages |= Page.objects.filter(
id__in=related_model._base_manager.filter(**{
relation.field.name: obj.id
}).values_list('id', flat=True)
)
else:
# if the relation is between obj and an object that has a page as a
# property, return the page
for f in related_model._meta.fields:
if isinstance(f, ParentalKey) and issubclass(f.remote_field.model, Page):
pages |= Page.objects.filter(
id__in=related_model._base_manager.filter(
**{
relation.field.name: obj.id
}).values_list(f.attname, flat=True)
)
return pages
示例6: get_object_usage
# 需要导入模块: from modelcluster import fields [as 别名]
# 或者: from modelcluster.fields import ParentalKey [as 别名]
def get_object_usage(obj):
"Returns a queryset of pages that link to a particular object"
pages = Page.objects.none()
# get all the relation objects for obj
relations = [f for f in type(obj)._meta.get_fields(include_hidden=True)
if (f.one_to_many or f.one_to_one or f.many_to_many) and f.auto_created]
for relation in relations:
related_model = relation.related_model
# if the relation is between obj and a page, get the page
if issubclass(related_model, Page):
pages |= Page.objects.filter(
id__in=related_model._base_manager.filter(**{
relation.field.name: obj.id
}).values_list('id', flat=True)
)
else:
# if the relation is between obj and an object that has a page as a
# property, return the page
for f in related_model._meta.fields:
if isinstance(f, ParentalKey) and issubclass(f.remote_field.model, Page):
pages |= Page.objects.filter(
id__in=related_model._base_manager.filter(
**{
relation.field.name: obj.id
}).values_list(f.attname, flat=True)
)
return pages