django+python+sqlite3

Author Avatar
Xzhah 11月 19, 2017
  • 在其它设备中阅读本文章

最近用django写了一个订单管理系统,记录一些代码,自己以后如果要用就拿来看

* 关于model

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
class CiOrder(models.Model):
class Meta:
db_table = 'ci_order' # 创建表名称
verbose_name = verbose_name_plural = '订单管理' # 表名称 别名 中文显示
ordering = ['order_number']
#def give_time():
#return datetime.date.today()
#def set_time():
#return datetime.datetime.now()
order_id = models.AutoField('ID', max_length=11, primary_key=True) # 字段显示别名
order_pname = models.CharField('产品名称',max_length=255,null=True,blank=True)
order_number = models.CharField('订单编码', max_length=255) # 字段显示中文别名
order_customer = models.CharField('客户', max_length=50, null=True, blank=True)
order_inprice = models.IntegerField('进货价格', max_length=11, null=True, blank=True)
order_brand = models.CharField('品牌', max_length=50, null=True, blank=True)
order_model = models.CharField('规格', max_length=50, null=True, blank=True)
order_quantity = models.IntegerField('订单数量', max_length=11)
order_unitprice = models.IntegerField('销售单价', max_length=11, null=True, blank=True)
order_owner = models.CharField('业务员', max_length=50, null=True, blank=True)
order_createdtime = models.DateTimeField('下单时间', default=timezone.now())
order_delivertime = models.DateTimeField('交付时间', default=timezone.now())
update_time = models.DateTimeField('更新时间', auto_now=True)
order_order_pid = models.ForeignKey(CiProduct,default=1)
GENDER_CHOICE1 = (#choices2 选项设置
(0,u'直接销售'),
(1,u'送货上门'),
)
order_alias = models.IntegerField('订单类型',choices=GENDER_CHOICE1,default=0,max_length=8, null=True, blank=True)
uniq_id = models.IntegerField(max_length=11, null=True, blank=True)
GENDER_CHOICE = (#choices2 选项设置
(0,u'有货'),
(1,u'没货'),
)
status = models.IntegerField('订单状态',choices=GENDER_CHOICE,default=0,max_length=8, null=True, blank=True)
def __str__(self):
return self.order_number
def total_price(self):
total_price=0
groceryItems = CiOrder.objects.all()
for item in groceryItems:
if(self.order_customer==item.order_customer):
if(self.order_createdtime.hour==item.order_createdtime.hour):
total_price += item.order_quantity*item.order_unitprice
return total_price
def day_sale(self):
time_sale=0
grceryItems1=CiOrder.objects.all()
for item in grceryItems1:
if(self.order_delivertime.day==item.order_delivertime.day):
time_sale+=1
return time_sale
def product_sale(self):
product_sale=0
grceryItems2=CiOrder.objects.all()
for item in grceryItems2:
if(self.order_order_pid==item.order_order_pid):
product_sale+= item.order_quantity*item.order_unitprice
return product_sale
def hour_sale(self):
hour_sale=0
grceryItems3=CiOrder.objects.all()
for item in grceryItems3:
if(self.order_createdtime.hour==item.order_createdtime.hour):
hour_sale+=1
return hour_sale

* 关于admin

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class ci_orderAdmin(admin.ModelAdmin): # 表ci_order用Admin权限添加显示字段和快速查询栏
list_display = ('order_number', 'order_pname','order_customer', 'order_inprice', 'order_brand', 'order_model' \
, 'order_quantity', 'order_unitprice', 'order_owner', 'order_createdtime' \
, 'order_delivertime', 'update_time','order_order_pid', 'order_alias', 'status','total_price','day_sale','product_sale','hour_sale') # 添加字段显示
search_fields = ('order_number', 'order_brand', 'order_customer','order_alias', 'order_inprice') # 添加快速查询栏
list_filter = ('order_number', 'order_brand', 'order_customer','order_alias', 'order_inprice') # 添加过滤器
list_per_page = 20 # 设置分页 每页面显示20行
list_editable = ['status'] # 设置默认可编辑字段
# fk_fields =('','')设置显示外键字段
# fieldsets = ( # # 分组表单
# ('基本信息', {'fields': ('title', 'content', 'excerpt', 'publish_date','status', 'user', 'categories')}),
# ('Meta Data', {'fields': ('alias', 'keywords', 'description')}),
# )

* 使用sqlite3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
#DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.mysql',
# 'NAME': 'order',
# 'USER': 'root',
# 'PASSWORD': '123.abc',
# 'HOST': 'localhost',
# 'PORT': '3306',
#
# }

* 成品展示

1.png 2.png 3.png 4.png 5.png 6.png 7.png

* 小结

自己是个二进制狗,很可能以后都不会再用到,这一次也只是为了完成数据库作业。所以,草草记录。