Python modelcluster.fields.ParentalKey() Examples
The following are 6
code examples of modelcluster.fields.ParentalKey().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
modelcluster.fields
, or try the search function
.
Example #1
Source File: test_cluster.py From django-modelcluster with BSD 3-Clause "New" or "Revised" License | 7 votes |
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.')
Example #2
Source File: test_cluster.py From django-modelcluster with BSD 3-Clause "New" or "Revised" License | 6 votes |
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")
Example #3
Source File: test_cluster.py From django-modelcluster with BSD 3-Clause "New" or "Revised" License | 6 votes |
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.")
Example #4
Source File: models.py From django-modelcluster with BSD 3-Clause "New" or "Revised" License | 5 votes |
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) ]
Example #5
Source File: models.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
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
Example #6
Source File: utils.py From wagtail-torchbox with MIT License | 5 votes |
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