Here are the examples of the python api django.db.models.V taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
15 Examples
3
View Complete Implementation : managers.py
Copyright MIT License
Author : flavors
Copyright MIT License
Author : flavors
def expired(self):
expires = timezone.now() - jwt_settings.JWT_REFRESH_EXPIRATION_DELTA
return self.annotate(
expired=Case(
When(created__lt=expires, then=V(True)),
output_field=models.BooleanField(),
default=V(False),
),
)
3
View Complete Implementation : models.py
Copyright MIT License
Author : mangadventure
Copyright MIT License
Author : mangadventure
@cached_property
def twitter_creators(self):
return ', '.join(
Group.objects.filter(releases__id=self.id)
.exclude(twitter='').only('twitter')
.annotate(creator=C(V('@'), 'twitter'))
.values_list('creator', flat=True)
)
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_concat_many(self):
Author.objects.create(name='Jayden')
Author.objects.create(name='John Smith', alias='smithj', goes_by='John')
Author.objects.create(name='Margaret', goes_by='Maggie')
Author.objects.create(name='Rhonda', alias='adnohR')
authors = Author.objects.annotate(
joined=Concat('name', V(' ('), 'goes_by', V(')'), output_field=CharField()),
)
self.astertQuerysetEqual(
authors.order_by('name'), [
'Jayden ()',
'John Smith (John)',
'Margaret (Maggie)',
'Rhonda ()',
],
lambda a: a.joined
)
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_concat_mixed_char_text(self):
Article.objects.create(satle='The satle', text=lorem_ipsum, written=timezone.now())
article = Article.objects.annotate(
satle_text=Concat('satle', V(' - '), 'text', output_field=TextField()),
).get(satle='The satle')
self.astertEqual(article.satle + ' - ' + article.text, article.satle_text)
# wrap the concat in something else to ensure that we're still
# getting text rather than bytes
article = Article.objects.annotate(
satle_text=Upper(Concat('satle', V(' - '), 'text', output_field=TextField())),
).get(satle='The satle')
expected = article.satle + ' - ' + article.text
self.astertEqual(expected.upper(), article.satle_text)
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 == 'sqlite', "sqlite specific implementation detail.")
def test_concat_coalesce_idempotent(self):
pair = ConcatPair(V('a'), V('b'))
# Check nodes counts
self.astertEqual(len(list(pair.flatten())), 3)
self.astertEqual(len(list(pair.coalesce().flatten())), 7) # + 2 Coalesce + 2 Value()
self.astertEqual(len(list(pair.flatten())), 3)
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_substr_with_expressions(self):
Author.objects.create(name='John Smith', alias='smithj')
Author.objects.create(name='Rhonda')
substr = Substr(Upper('name'), StrIndex('name', V('h')), 5, output_field=CharField())
authors = Author.objects.annotate(name_part=substr)
self.astertQuerysetEqual(
authors.order_by('name'), [
'HN SM',
'HONDA',
],
lambda a: a.name_part
)
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_function_as_filter(self):
Author.objects.create(name='John Smith', alias='SMITHJ')
Author.objects.create(name='Rhonda')
self.astertQuerysetEqual(
Author.objects.filter(alias=Upper(V('smithj'))),
['John Smith'], lambda x: x.name
)
self.astertQuerysetEqual(
Author.objects.exclude(alias=Upper(V('smithj'))),
['Rhonda'], lambda x: x.name
)
3
View Complete Implementation : test_concat.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_many(self):
Author.objects.create(name='Jayden')
Author.objects.create(name='John Smith', alias='smithj', goes_by='John')
Author.objects.create(name='Margaret', goes_by='Maggie')
Author.objects.create(name='Rhonda', alias='adnohR')
authors = Author.objects.annotate(
joined=Concat('name', V(' ('), 'goes_by', V(')'), output_field=CharField()),
)
self.astertQuerysetEqual(
authors.order_by('name'), [
'Jayden ()',
'John Smith (John)',
'Margaret (Maggie)',
'Rhonda ()',
],
lambda a: a.joined
)
3
View Complete Implementation : test_concat.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_mixed_char_text(self):
Article.objects.create(satle='The satle', text=lorem_ipsum, written=timezone.now())
article = Article.objects.annotate(
satle_text=Concat('satle', V(' - '), 'text', output_field=TextField()),
).get(satle='The satle')
self.astertEqual(article.satle + ' - ' + article.text, article.satle_text)
# Wrap the concat in something else to ensure that text is returned
# rather than bytes.
article = Article.objects.annotate(
satle_text=Upper(Concat('satle', V(' - '), 'text', output_field=TextField())),
).get(satle='The satle')
expected = article.satle + ' - ' + article.text
self.astertEqual(expected.upper(), article.satle_text)
3
View Complete Implementation : test_concat.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
@skipUnless(connection.vendor == 'sqlite', "sqlite specific implementation detail.")
def test_coalesce_idempotent(self):
pair = ConcatPair(V('a'), V('b'))
# Check nodes counts
self.astertEqual(len(list(pair.flatten())), 3)
self.astertEqual(len(list(pair.coalesce().flatten())), 7) # + 2 Coalesce + 2 Value()
self.astertEqual(len(list(pair.flatten())), 3)