def contribute_to_class(self, cls, name): if self.ct_field is None: self.ct_field = '%s_content_type' % name if self.fk_field is None: self.fk_field = '%s_object_id' % name # do not do anything that loads the app cache here, like # _meta.get_all_field_names, or you'll cause a circular import. if not model_has_field(cls, self.ct_field): ct_field = models.ForeignKey(ContentType, related_name='+', null=self.null, editable=False) cls.add_to_class(self.ct_field, ct_field) if not model_has_field(cls, self.fk_field): fk_field = models.PositiveIntegerField(null=self.null, editable=False) cls.add_to_class(self.fk_field, fk_field) super(LinkField, self).contribute_to_class(cls, name)
def test_for_non_existent_field_on_child_model(self): assert not model_has_field(ChildFieldExistsModel, 'does_not_exist') assert not model_has_field(ChildFieldExistsModel, 'does_not_exist')
def test_for_existing_field_on_child_model(self): assert model_has_field(ChildFieldExistsModel, 'field_a') assert model_has_field(ChildFieldExistsModel, 'field_b')
def test_for_non_existent_field(self): assert not model_has_field(FieldExistsModel, 'does_not_exist')
def test_for_existing_field(self): assert model_has_field(FieldExistsModel, 'field_a')