예제 #1
0
파일: tests.py 프로젝트: EmadMokhtar/django
 def test_multiple_conditions(self):
     with connection.schema_editor() as editor:
         index = Index(
             name='recent_article_idx',
             fields=['pub_date', 'headline'],
             condition=(
                 Q(pub_date__gt=datetime.datetime(
                     year=2015,
                     month=1,
                     day=1,
                     tzinfo=timezone.get_current_timezone(),
                 )) & Q(headline__contains='China')
             ),
         )
         sql = str(index.create_sql(Article, schema_editor=editor))
         where = sql.find('WHERE')
         self.assertIn(
             'WHERE (%s.%s' % (editor.quote_name(Article._meta.db_table), editor.quote_name("pub_date")),
             sql
         )
         # Because each backend has different syntax for the operators,
         # check ONLY the occurrence of headline in the SQL.
         self.assertGreater(sql.rfind('headline'), where)
         editor.add_index(index=index, model=Article)
         self.assertIn(index.name, connection.introspection.get_constraints(
             cursor=connection.cursor(), table_name=Article._meta.db_table,
         ))
         editor.remove_index(index=index, model=Article)
예제 #2
0
파일: tests.py 프로젝트: EmadMokhtar/django
    def test_condition_ignored(self):
        index = Index(
            name='test_condition_ignored',
            fields=['published'],
            condition=Q(published=True),
        )
        with connection.schema_editor() as editor:
            # This would error if condition weren't ignored.
            editor.add_index(Article, index)

        self.assertNotIn(
            'WHERE %s.%s' % (editor.quote_name(Article._meta.db_table), 'published'),
            str(index.create_sql(Article, editor))
        )
예제 #3
0
파일: tests.py 프로젝트: GravyHands/django
 def test_boolean_restriction_partial(self):
     with connection.schema_editor() as editor:
         index = Index(
             name='published_index',
             fields=['published'],
             condition=Q(published=True),
         )
         self.assertIn(
             'WHERE %s.%s' % (editor.quote_name(Article._meta.db_table), editor.quote_name('published')),
             str(index.create_sql(Article, schema_editor=editor))
         )
         editor.add_index(index=index, model=Article)
         self.assertIn(index.name, connection.introspection.get_constraints(
             cursor=connection.cursor(), table_name=Article._meta.db_table,
         ))
예제 #4
0
파일: tests.py 프로젝트: GravyHands/django
 def test_is_null_condition(self):
     with connection.schema_editor() as editor:
         index = Index(
             name='recent_article_idx',
             fields=['pub_date'],
             condition=Q(pub_date__isnull=False),
         )
         self.assertIn(
             'WHERE %s.%s IS NOT NULL' % (editor.quote_name(Article._meta.db_table), editor.quote_name("pub_date")),
             str(index.create_sql(Article, schema_editor=editor))
         )
         editor.add_index(index=index, model=Article)
         self.assertIn(index.name, connection.introspection.get_constraints(
             cursor=connection.cursor(), table_name=Article._meta.db_table,
         ))
예제 #5
0
파일: tests.py 프로젝트: EmadMokhtar/django
 def test_ops_class_partial_tablespace(self):
     indexname = 'test_ops_class_tblspace'
     index = Index(
         name=indexname,
         fields=['body'],
         opclasses=['text_pattern_ops'],
         condition=Q(headline__contains='China'),
         db_tablespace='pg_default',
     )
     with connection.schema_editor() as editor:
         editor.add_index(IndexedArticle2, index)
         self.assertIn('TABLESPACE "pg_default" ', str(index.create_sql(IndexedArticle2, editor)))
     with editor.connection.cursor() as cursor:
         cursor.execute(self.get_opclass_query % indexname)
         self.assertCountEqual(cursor.fetchall(), [('text_pattern_ops', indexname)])
예제 #6
0
파일: tests.py 프로젝트: EmadMokhtar/django
 def test_integer_restriction_partial(self):
     with connection.schema_editor() as editor:
         index = Index(
             name='recent_article_idx',
             fields=['id'],
             condition=Q(pk__gt=1),
         )
         self.assertIn(
             'WHERE %s.%s' % (editor.quote_name(Article._meta.db_table), editor.quote_name('id')),
             str(index.create_sql(Article, schema_editor=editor))
         )
         editor.add_index(index=index, model=Article)
         self.assertIn(index.name, connection.introspection.get_constraints(
             cursor=connection.cursor(), table_name=Article._meta.db_table,
         ))
         editor.remove_index(index=index, model=Article)
예제 #7
0
파일: tests.py 프로젝트: GravyHands/django
 def test_partial_index(self):
     with connection.schema_editor() as editor:
         index = Index(
             name='recent_article_idx',
             fields=['pub_date'],
             condition=Q(
                 pub_date__gt=datetime.datetime(
                     year=2015, month=1, day=1,
                     # PostgreSQL would otherwise complain about the lookup
                     # being converted to a mutable function (by removing
                     # the timezone in the cast) which is forbidden.
                     tzinfo=timezone.get_current_timezone(),
                 ),
             )
         )
         self.assertIn(
             'WHERE %s.%s' % (editor.quote_name(Article._meta.db_table), editor.quote_name("pub_date")),
             str(index.create_sql(Article, schema_editor=editor))
         )
         editor.add_index(index=index, model=Article)
         self.assertIn(index.name, connection.introspection.get_constraints(
             cursor=connection.cursor(), table_name=Article._meta.db_table,
         ))
예제 #8
0
 class Meta:
     indexes = [
         Index(fields=["activity"]),
         Index(fields=["pl", "user"])
     ]
예제 #9
0
from __future__ import unicode_literals

from django.db.models import F, Q
from django.db.models.options import Options

try:
    # Django >= 1.7
    from django import apps
except ImportError:
    # Django < 1.7
    apps = None

try:
    # Django >= 1.11
    from django.db.models import Index
    _test_index = Index(fields=['test'])
except ImportError:
    Index = None
    _test_index = None

_options = Options({})

#: Index names changed in Django 1.5, with the introduction of index_together.
supports_index_together = hasattr(_options, 'index_together')

#: Whether new-style Index classes are available.
#:
#: Django 1.11 introduced formal support for defining explicit indexes not
#: bound to a field definition or as part of
#: ``index_together``/``unique_together``.
#:
예제 #10
0
 class Meta:
     unique_together = (
         "facility",
         "item",
     )
     indexes = [Index(fields=("facility", "item",))]
예제 #11
0
 class Meta:
     indexes = [Index(fields=['address', '-nonce'])
                ]  # Index on address and nonce DESC
     unique_together = (('internal_tx', 'address'), )
     verbose_name_plural = 'Safe statuses'
예제 #12
0
 class Meta:
     indexes = [Index(fields=['-created'])]
     unique_together = (('recipe', 'user_list'), )
예제 #13
0
    class Meta:
        indexes = [Index(fields=('name', ))]

        verbose_name = _('Color')
        verbose_name_plural = _('Colors')
예제 #14
0
 class Meta:
     ordering = ('name', )
     indexes = [Index(fields=('name', ))]
     verbose_name = _('Car brand')
     verbose_name_plural = _('Car brands')
예제 #15
0
 class Meta:
     indexes = [
         Index(fields=['-rating', 'datetime']),  # default index is desc
     ]
예제 #16
0
 class Meta:
     ordering = ("name", )
     indexes = [Index(fields=("name", ))]
     verbose_name = _("Car brand")
     verbose_name_plural = _("Car brands")
예제 #17
0
 class Meta:
     indexes = [Index(fields=['keyword'])]
     constraints = [
         UniqueConstraint(fields=['keyword', 'recipe'],
                          name='unique_keyword'),
     ]
예제 #18
0
 class Meta:
     indexes = [
         Index(fields=['movie']),
         Index(fields=['date']),
     ]
예제 #19
0
 class Meta:
     verbose_name = _('Order')
     verbose_name_plural = _('Orders')
     indexes = [Index(fields=['status', ])]
예제 #20
0
 class Meta:
     indexes = (Index(fields=("classifier", "-created_on")),
                Index(fields=("ticketer", "-created_on")))
예제 #21
0
 class Meta:
     indexes = [Index(fields=['post', 'creator'])]
예제 #22
0
    class Meta:
        indexes = [Index(fields=("name", ))]

        verbose_name = _("Color")
        verbose_name_plural = _("Colors")
예제 #23
0
파일: models.py 프로젝트: bkalika/cursor
 class Meta:
     ordering = ['first_name', 'last_name']
     indexes = [
         Index(fields=['first_name', 'last_name']),
     ]