示例#1
0
    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)
示例#2
0
    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)
示例#3
0
 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')
示例#4
0
 def test_for_existing_field_on_child_model(self):
     assert model_has_field(ChildFieldExistsModel, 'field_a')
     assert model_has_field(ChildFieldExistsModel, 'field_b')
示例#5
0
 def test_for_non_existent_field(self):
     assert not model_has_field(FieldExistsModel, 'does_not_exist')
示例#6
0
 def test_for_existing_field(self):
     assert model_has_field(FieldExistsModel, 'field_a')
示例#7
0
 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')
示例#8
0
 def test_for_existing_field_on_child_model(self):
     assert model_has_field(ChildFieldExistsModel, 'field_a')
     assert model_has_field(ChildFieldExistsModel, 'field_b')
示例#9
0
 def test_for_non_existent_field(self):
     assert not model_has_field(FieldExistsModel, 'does_not_exist')
示例#10
0
 def test_for_existing_field(self):
     assert model_has_field(FieldExistsModel, 'field_a')