Vue+Django(DRF)独立开发电商项目

本文深入探讨了Vue与Django Rest Framework(DRF)在电商项目中的高级应用,包括Django的ORM操作如queryset的进阶和高级特性,如select_related、prefetch_related、defer和only等提高查询效率的方法。同时介绍了Vue的基础知识,包括组件、路由、CLI环境搭建和实际项目的实践。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

基础篇

django-orm操作

进阶操作

获取个数
#
models.Tb1.objects.filter(name='seven').count()

大于,小于
#
models.Tb1.objects.filter(id__gt=1)              获取id大于1的值
models.Tb1.objects.filter(id__gte=1)              获取id大于等于1的值
models.Tb1.objects.filter(id__lt=10)             获取id小于10的值
models.Tb1.objects.filter(id__lte=10)             获取id小于10的值
models.Tb1.objects.filter(id__lt=10, id__gt=1)   获取id大于1 且 小于10的值

in
#
models.Tb1.objects.filter(id__in=[11, 22, 33])   获取id等于11、22、33的数据
models.Tb1.objects.exclude(id__in=[11, 22, 33])  not in

isnull
Entry.objects.filter(pub_date__isnull=True)

contains
#
models.Tb1.objects.filter(name__contains="ven")
models.Tb1.objects.filter(name__icontains="ven") icontains大小写不敏感
models.Tb1.objects.exclude(name__icontains="ven")

range
#
models.Tb1.objects.filter(id__range=[1, 2])   范围bettwen and

其他类似
#
startswith,istartswith, endswith, iendswith,

order by
#
models.Tb1.objects.filter(name='seven').order_by('id')    asc
models.Tb1.objects.filter(name='seven').order_by('-id')   desc

group by
#
from django.db.models import Count, Min, Max, Sum
models.Tb1.objects.filter(c1=1).values('id').annotate(c=Count('num'))
SELECT "app01_tb1"."id", COUNT("app01_tb1"."num") AS "c" FROM "app01_tb1" WHERE "app01_tb1"."c1" = 1 GROUP BY "app01_tb1"."id"

limit 、offset
#
models.Tb1.objects.all()[10:20]

regex正则匹配,iregex 不区分大小写
#
Entry.objects.get(title__regex=r'^(An?|The) +')
Entry.objects.get(title__iregex=r'^(an?|the) +')

date
#
Entry.objects.filter(pub_date__date=datetime.date(2005, 1, 1))
Entry.objects.filter(pub_date__date__gt=datetime.date(2005, 1, 1))

year
#
Entry.objects.filter(pub_date__year=2005)
Entry.objects.filter(pub_date__year__gte=2005)

month
#
Entry.objects.filter(pub_date__month=12)
Entry.objects.filter(pub_date__month__gte=6)

day
#
Entry.objects.filter(pub_date__day=3)
Entry.objects.filter(pub_date__day__gte=3)

week_day
#
Entry.objects.filter(pub_date__week_day=2)
Entry.objects.filter(pub_date__week_day__gte=2)

hour
#
Event.objects.filter(timestamp__hour=23)
Event.objects.filter(time__hour=5)
Event.objects.filter(timestamp__hour__gte=12)

minute
#
Event.objects.filter(timestamp__minute=29)
Event.objects.filter(time__minute=46)
Event.objects.filter(timestamp__minute__gte=29)

second
#
Event.objects.filter(timestamp__second=31)
Event.objects.filter(time__second=2)
Event.objects.filter(timestamp__second__gte=31)

高级操作

extra 
在QuerySet的基础上继续执行子语句 

extra(self, select=None, where=None, params=None, tables=None, order_by=None, select_params=None) 
select-select_params

例子:

tmp = models.Book.objects.all().extra(select={"n":"select count(1) from app05_book"})
for i in tmp:
    print(i.title, i.n)
书一 6
书二 6
书三(第) 6
书四(第) 6
书五(第) 6
书六(第) 6
1
2
3
4
5
6
7
8
9


select占位符

tmp = models.Book.objects.all().extra(select={"n":"select count(1) from app05_book WHERE id>%s"},select_params=[3,])
for i in tmp:
    print(i.title, i.n)
书一 3
书二 3
书三(第) 3
书四(第) 3
书五(第) 3
书六(第) 3
1
2
3
4
5
6
7
8
9


where-params

models.Book.objects.extra(where=["title=%s"],params=["书一"])
<QuerySet [<Book: 书一>]>
1
2
models.Book.objects.extra(where=["title='书一' or title='书二'"])
<QuerySet [<Book: 书一>, <Book: 书二>]>
1
2


and关系

models.Book.objects.extra(where=["title='书一'","id=2"])
<QuerySet []>
1
2
3


举个例子:

models.UserInfo.objects.extra(
                    select={'newid':'select count(1) from app01_usertype where id>%s'},
                    select_params=[1,],
                    where = ['age>%s'],
                    params=[18,],
                    order_by=['-age'],
                    tables=['app01_usertype']
                )
                """
                select 
                    app01_userinfo.id,
    &nb
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值