Opened 5 years ago

Closed 5 years ago

#31074 closed Uncategorized (invalid)

Errors model abstract testing.

Reported by: Artem Owned by: nobody
Component: Database layer (models, ORM) Version: 3.0
Severity: Normal Keywords: test, ManyToManyField, abstract, model
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Artem)

I have models:

class Country(models.Model):
    name = models.CharField(_("Country name"), max_length=50)
    code = models.SlugField(_("Country iso-2 code"), max_length=2, unique=True)
class CountryModelMixin(models.Model):
    countries_admin = models.ManyToManyField(
    countries = ArrayField(
        models.SlugField(verbose_name=_("Country iso-2 code"), max_length=2),
        blank=True, null=True,

    class Meta(object):
        abstract = True

    def update_hidden(sender, instance, **kwargs):
        if issubclass(instance.__class__, CountryModelMixin):
               instance.countries = countries.countries_admin.values_list('code', flat=True)

class Orgaization(CountryModelMixin):
    name = models.CharField(_("Name"), max_length=127)
    slug = models.SlugField(_("Slug"), max_length=127, unique=True)
    active = models.BooleanField(_("Active"), default=True)

    address = models.CharField(_("Address"), max_length=256, null=True, blank=True)

And I need testing CountryModelMixin models:
I create test:

class ModelMixinTestCase(TestCase):
    mixin= CountryModelMixin

    def setUpClass(cls):
        if not cls.mixin:
        if isinstance(cls.mixin, list):
            name = ""
            for mxn in cls.mixin:
                name += mxn.__name__
            name = hashlib.md5(name.encode('UTF-8')).hexdigest()
            cls.model = ModelBase('__TestModel__' + name, tuple(cls.mixin),
                                  {'__module__': cls.mixin[0].__module__})
            cls.model = ModelBase('__TestModel__' +
                cls.mixin.__name__, (cls.mixin,),
                {'__module__': cls.mixin.__module__}

        # Create the schema for  our test model
        with connection.schema_editor() as schema_editor:
        super(ModelMixinTestCase, cls).setUpClass()

    def tearDownClass(cls):
        if not cls.mixin:
        # Delete the schema for the test model
        with connection.schema_editor() as schema_editor:
        super(ModelMixinTestCase, cls).tearDownClass()

       def setUp(self):
      = Country.objects.create(name="Test", code="EN")

       def test_go(self):
            myModelElement = self.model.objects.create()
            myModelElement.countries_admin.set([, ])

return self.instance._prefetched_objects_cache[self.prefetch_cache_name]
AttributeError: 'TestModel35ecfa746aba30e3344f3534dd7552ac' object has no attribute '_prefetched_objects_cache'

Test return errors: Cannot resolve keyword "" into field.
And delete myModelElement.delete() - don't work

Test abstract models without ManyToManyField work goods.

Change History (3)

comment:1 by Artem, 5 years ago

Description: modified (diff)

comment:2 by Artem, 5 years ago

Description: modified (diff)

comment:3 by Mariusz Felisiak, 5 years ago

Component: UncategorizedDatabase layer (models, ORM)
Resolution: invalid
Status: newclosed
Summary: Errors model abstract testingErrors model abstract testing.

As far as I'm concerned this is not an issue in Django. IMO you should use Model instead of BaseModel, nevertheless please don't use Trac as a support channel.

Closing per TicketClosingReasons/UseSupportChannels.

Note: See TracTickets for help on using tickets.
Back to Top