import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "test.setting")
import django
django.setup()
# from django.core.management import call_command
#
#
# call_command("test", "")
from django.db import models
from chat.models import Dog
from django.db.models.functions import Cast
Dog.objects.create(name="Meg", age=1, data={"breed": "collie", "owner": {"name": "Tom", "age": 10}}, food=["noodles", "fuck"])
Dog.objects.create(name="WangWang", age=1, data={"breed": "collie", "owner": {"name": "LiMing", "age": 10}}, food=["bone", ])
Dog.objects.annotate(
title=Cast("data__breed", models.TextField()),
).filter(title__regex="c.*")
Dog.objects.annotate(
title=Cast("food__*", models.TextField()),
).filter(title__contains="nood")
from chat.models import Cat
Dog.objects.annotate(
title=Cast("data__owner__name", models.TextField()),
).filter(title__regex="t.*")
Dog.objects.annotate(
title=Cast("food", models.TextField()),
).filter(title__regex="t.*")
Cat.objects.annotate(
title=Cast("food__0", models.TextField())
).values("title")
# Cat.objects.annotate(
# food="food"
# ).filter(food="11")
Dog.objects.filter(data__owner__age__gt=1)
from django.db.models import Q
from django.utils import timezone
Dog.objects.filter(create_time__lt=timezone.now())
Dog.objects.filter(name__icontains="e")
Dog.objects.filter(name__startswith="M") # 区分大小
Dog.objects.filter(name__istartswith="M") # 不区分大小写
Dog.objects.filter(name__endswith="g") # 区分大小
Dog.objects.filter(name__iendswith="g") # 不区分大小写
Dog.objects.filter(name__regex=".*?g") # 区分大小写
Dog.objects.filter(name__iregex=".*?G") # 不区分大小写
Dog.objects.filter(age__exact=1)
Dog.objects.filter(age__in=[1, 2, 3])
Dog.objects.exclude(age=1)
Dog.objects.filter(~Q(age=1))
Dog.objects.filter(age__gt=1) # 大于
Dog.objects.filter(age__gte=1) # 大于等于
Dog.objects.filter(age__lt=2) # 小于
Dog.objects.filter(age__lte=2) # 小于等于
Dog.objects.filter(create_time__gt=timezone.now()) # 大于
Dog.objects.filter(create_time__gte=timezone.now()) # 大于等于
Dog.objects.filter(create_time__lt=timezone.now()) # 小于
Dog.objects.filter(create_time__lte=timezone.now()) # 小于等于
Dog.objects.filter(food__contains="noodles")
Dog.objects.filter(~Q(food__contains="noodles"))
Dog.objects.filter(age__in=[1, 2, 4])
Dog.objects.filter(age__range=(1, 10))
Dog.objects.annotate(
title=Cast("food", models.TextField()),
).filter(title__regex="t.*")
from chat.models import Cat
Cat.objects.annotate(title=Cast("food", models.TextField())).filter(title__regex="1.*")
Cat.objects.filter(food__0__contains="1")
Cat.objects.annotate(
title=Cast("food", models.TextField()),
).filter(title__contains="11,")
# 精确查找
Cat.objects.annotate(
title=Cast("food", models.TextField()),
).filter(title__regex="")
value = "11"
# rf"[\{{|,]{value}[,|\}}]"
Cat.objects.annotate(
title=Cast("food", models.TextField()),
).filter(title__regex=rf"[\{{|,]{value}[,|\}}]")
q = Q()
for item in ["{{item},", ",{item}},", ",{item},", "{{item}}"]:
q |= Q(title__contains=item.replace("{item}","11"))
Cat.objects.annotate(
title=Cast("food", models.TextField()),
).filter(q)
from django.contrib.postgres.fields import ArrayField
Cat.objects.annotate(
title=Cast("food", ArrayField(models.TextField())),
)
# .filter(q)
# FloatField
Dog.objects.annotate(
food=Cast("data__owner__age", models.IntegerField()),
).filter(food=10)
from django.db.models import F, Value, TextField
from django.db.models.expressions import Func
from django.contrib.postgres.fields import ArrayField
Cat.objects.annotate(
some_field_splitted=Func(
F('food'),
Value(","),
function='regexp_split_to_array',
output_field=ArrayField(TextField())
)
).filter()
# Dog.objects.annotate(
# # some_field_splitted=Cast("food",Func(
# # # F('food'),
# # # Value(","),
# # function='regexp_split_to_array',
# # output_field=ArrayField(TextField())
# # )
# # ))
from django.contrib.postgres.fields import JSONField, ArrayField
Dog.objects.annotate(
title=Cast("food__any", ArrayField(JSONField(models.TextField()))),
).filter(title__0="1")