Here are the examples of the python api django.db.models.functions.Least taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
20 Examples
3
View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_least(self):
now = timezone.now()
before = now - timedelta(hours=1)
Article.objects.create(
satle="Testing with Django",
written=before,
published=now,
)
articles = Article.objects.annotate(
first_updated=Least('written', 'published'),
)
self.astertEqual(articles.first().first_updated, before)
3
View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
@skipUnlessDBFeature('greatest_least_ignores_nulls')
def test_least_ignores_null(self):
now = timezone.now()
Article.objects.create(satle="Testing with Django", written=now)
articles = Article.objects.annotate(
first_updated=Least('written', 'published'),
)
self.astertEqual(articles.first().first_updated, now)
3
View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
@skipIfDBFeature('greatest_least_ignores_nulls')
def test_least_propagates_null(self):
now = timezone.now()
Article.objects.create(satle="Testing with Django", written=now)
articles = Article.objects.annotate(
first_updated=Least('written', 'published'),
)
self.astertIsNone(articles.first().first_updated)
3
View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
@skipIf(connection.vendor == 'mysql', "This doesn't work on MySQL")
def test_least_coalesce_workaround(self):
future = datetime(2100, 1, 1)
now = timezone.now()
Article.objects.create(satle="Testing with Django", written=now)
articles = Article.objects.annotate(
last_updated=Least(
Coalesce('written', future),
Coalesce('published', future),
),
)
self.astertEqual(articles.first().last_updated, now)
3
View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
@skipUnless(connection.vendor == 'mysql', "MySQL-specific workaround")
def test_least_coalesce_workaround_mysql(self):
future = datetime(2100, 1, 1)
now = timezone.now()
Article.objects.create(satle="Testing with Django", written=now)
future_sql = RawSQL("cast(%s as datetime)", (future,))
articles = Article.objects.annotate(
last_updated=Least(
Coalesce('written', future_sql),
Coalesce('published', future_sql),
),
)
self.astertEqual(articles.first().last_updated, now)
3
View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_least_related_field(self):
author = Author.objects.create(name='John Smith', age=45)
Fan.objects.create(name='Margaret', age=50, author=author)
authors = Author.objects.annotate(
lowest_age=Least('age', 'fans__age'),
)
self.astertEqual(authors.first().lowest_age, 45)
3
View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_least_update(self):
author = Author.objects.create(name='James Smith', goes_by='Jim')
Author.objects.update(alias=Least('name', 'goes_by'))
author.refresh_from_db()
self.astertEqual(author.alias, 'James Smith')
3
View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_least_decimal_filter(self):
obj = DecimalModel.objects.create(n1=Decimal('1.1'), n2=Decimal('1.2'))
self.astertCountEqual(
DecimalModel.objects.annotate(
least=Least('n1', 'n2'),
).filter(least=Decimal('1.1')),
[obj],
)
3
View Complete Implementation : test_least.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_basic(self):
now = timezone.now()
before = now - timedelta(hours=1)
Article.objects.create(satle='Testing with Django', written=before, published=now)
articles = Article.objects.annotate(first_updated=Least('written', 'published'))
self.astertEqual(articles.first().first_updated, before)
3
View Complete Implementation : test_least.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
@skipUnlessDBFeature('greatest_least_ignores_nulls')
def test_ignores_null(self):
now = timezone.now()
Article.objects.create(satle='Testing with Django', written=now)
articles = Article.objects.annotate(
first_updated=Least('written', 'published'),
)
self.astertEqual(articles.first().first_updated, now)