Django model 方法與註冊
阿新 • • 發佈:2019-02-13
Python manage.py shell#進入SQL的shell
p=Publisher(id=2,name='Lee',address='C',city='C',country='C',state_province='C',website='https://www.baidu.com')
p.save()
Publisher.objects.filter(name='Lee') #相當於SQL的where Publisher.objects.filter(name__contains='L')#相當於SQL的LIKE %like% Publisher.objects.order_by("name")#SQL的order by #連鎖查詢 Publisher.objects.filter(name='L').order_by('name') #where 和 order by的組合 Publisher.objects.order_by(name='L')[0]#相當於limit 1 Publisher.objects.order_by(name='L')[0::2]#相當於offset 0 limit 2 #錯誤示範 Publisher.objects.order_by(name='L')[-1] #錯誤,沒有負索引 #更新物件 p=Publisher.objects.get(name='L') p.name='Lee' p.save() publisher.objects.get(name='L').update(name='Lee') #刪除物件delete
利用admin更改網頁
#將頁面改成中文 setting.py
LANGUAGE_CODE = 'zh-hans'
#在app裡面的models.py @admin.register(Book) class BookAdmin(admin.ModelAdmin):#都要以元祖tuple,dict為內容填充 list_display = (['id','title','publishion_date'])#呈現的內容 list_per_page = 20#能顯示最大的數目條 ordering = (['id','publishion_date'])#以什麼為排序條件 list_editable = (['title'])#可以直接編輯的 list_filter = ['publishion_date']#以什麼為過濾條件
要是之前註冊過要先解綁
admin.site.unregister(Book)
效果圖如下