Dự án Django này là một ví dụ triển khai cơ sở dữ liệu BikeStores, ban đầu được thiết kế cho các bài học SQL Server. Dự án sử dụng cơ sở dữ liệu mặc định SQLite của Django và bao gồm các app: production và sales. Mỗi app chứa các model liên quan.
Làm theo các bước sau để thiết lập dự án trên máy của bạn:
git clone https://github.com/ten-ban/bike_stores.git
cd bike_storespython -m venv venv
venv\Scripts\activateSau khi activate virtual environment, cài đặt thư viện trong file requirements.txt:
pip install -r requirements.txtSao chép file mẫu .env.example thành .env:
copy .env.example .env # Windowshoặc
cp .env.example .env # Linux/macOSpython manage.py runserver- Truy cập trang chủ tại: http://127.0.0.1:8000/
- Truy cập trang admin tại: http://127.0.0.1:8000/admin
Để kiểm tra các chức năng của dự án, bạn có thể chạy toàn bộ test bằng lệnh:
python manage.py test- Username:
admin - Password:
12345678
Bạn có thể truy vấn trực tiếp cơ sở dữ liệu db.sqlite3 bằng công cụ dòng lệnh sqlite3.
-
Mở terminal của bạn và di chuyển đến thư mục gốc của dự án (nơi chứa file
manage.pyvàdb.sqlite3). Ví dụ:cd path/to/bike_stores -
Kết nối với cơ sở dữ liệu:
sqlite3 db.sqlite3
Lệnh này sẽ mở dấu nhắc của SQLite (
sqlite>). -
Hiển thị các bảng có sẵn:
.tables -
Chạy một truy vấn đơn giản: Để chọn tất cả dữ liệu từ một bảng (ví dụ: một bảng có tên
customershoặc tên bảng cụ thể của bạn nhưproduction_customer):SELECT * FROM customers;Lưu ý quan trọng: Tất cả các câu lệnh truy vấn SQL phải kết thúc bằng dấu chấm phẩy (
;) để chúng có thể thực thi trongsqlite3. Nếu bạn nhấn Enter mà không có dấu chấm phẩy, trình bao thường sẽ hiển thị dấu nhắc tiếp tục (như...>) để chờ bạn hoàn thành lệnh. -
Thoát khỏi SQLite:
.quit
Lưu ý: Nếu lệnh .tables không hiển thị gì hoặc các bảng của ứng dụng Django bị thiếu, có thể bạn cần chạy Django migrations trước:
python manage.py makemigrations
python manage.py migrateQuản lý các model liên quan đến sản phẩm:
-
Category:category_idcategory_name
-
Brand:brand_idbrand_name
-
Product:product_idproduct_namebrand_id(FK →Brand)category_id(FK →Category)model_yearlist_price
-
Stock:store_id(FK →Store)product_id(FK →Product)quantity- Primary key: (
store_id,product_id)
Quản lý khách hàng, nhân viên, cửa hàng và đơn hàng:
-
Customer:customer_idfirst_name,last_name,phone,emailstreet,city,state,zip_code
-
Store:store_idstore_name,phone,emailstreet,city,state,zip_code
-
Staff:staff_idfirst_name,last_name,email,phoneactivestore_id(FK →Store)manager_id(FK → chínhStaff)
-
Order:order_idcustomer_id(FK →Customer)order_status(1=Pending, 2=Processing, 3=Rejected, 4=Completed)order_date,required_date,shipped_datestore_id(FK →Store)staff_id(FK →Staff)
-
OrderItem:order_id(FK →Order)item_idproduct_id(FK →Product)quantitylist_pricediscount- Primary key: (
order_id,item_id)
rm db.sqlite3
rm production/migrations/00*.py
rm sales/migrations/00*.py
python3 manage.py makemigrations
python3 manage.py migrate
python3 manage.py createsuperuser
sqlite3 db.sqlite3 < load_data_modified_vie.sql