#31074: Errors model abstract testing
-------------------------------------+-------------------------------------
               Reporter:  Artem      |          Owner:  nobody
                   Type:             |         Status:  new
  Uncategorized                      |
              Component:             |        Version:  3.0
  Uncategorized                      |       Keywords:  test,
               Severity:  Normal     |  ManyToManyField, abstract, model
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 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(
         Country,
         verbose_name="Страны",
         related_name="%(app_label)s_%(class)s_related",
         related_query_name="%(app_label)s_%(class)ss",
     )
     countries = ArrayField(
         models.SlugField(verbose_name=_("Country iso-2 code"),
 max_length=2),
         verbose_name=_("Countries"),
         blank=True, null=True,
     )

     class Meta(object):
         abstract = True

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

 m2m_changed.connect(CountryModelMixin.update_hidden)
 }}}

 {{{
 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

     @classmethod
     def setUpClass(cls):
         if not cls.mixin:
             return
         settings.MEDIA_ROOT = tempfile.mkdtemp()
         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__})
         else:
             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:
             schema_editor.create_model(cls.model)
         super(ModelMixinTestCase, cls).setUpClass()

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

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

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

 Test abstract models without ManyToManyField work goods.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/31074>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/048.6c3f80a7c74b276114345c2342ad9204%40djangoproject.com.

Reply via email to