claude-code-templates 1.0.1 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -14
- package/package.json +1 -1
- package/src/command-scanner.js +175 -0
- package/src/file-operations.js +34 -2
- package/src/prompts.js +47 -1
- package/src/templates.js +10 -1
- package/templates/common/.claude/commands/git-workflow.md +239 -0
- package/templates/common/.claude/commands/project-setup.md +316 -0
- package/templates/javascript-typescript/.claude/commands/route.md +193 -0
- package/templates/python/.claude/commands/django-model.md +124 -0
- package/templates/python/.claude/commands/flask-route.md +217 -0
- package/templates/python/.claude/commands/lint.md +111 -0
- package/templates/python/.claude/commands/test.md +73 -0
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
# Project Setup Helper
|
|
2
|
+
|
|
3
|
+
Set up new projects with proper structure and best practices.
|
|
4
|
+
|
|
5
|
+
## Purpose
|
|
6
|
+
|
|
7
|
+
This command helps you set up new projects with proper directory structure, configuration files, and development environment.
|
|
8
|
+
|
|
9
|
+
## Usage
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
/project-setup
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## What this command does
|
|
16
|
+
|
|
17
|
+
1. **Creates project structure** with standard directories
|
|
18
|
+
2. **Sets up configuration files** (.gitignore, README, etc.)
|
|
19
|
+
3. **Initializes version control** and development tools
|
|
20
|
+
4. **Configures environment files** and dependencies
|
|
21
|
+
5. **Follows language-specific** conventions and best practices
|
|
22
|
+
|
|
23
|
+
## Project Structure Templates
|
|
24
|
+
|
|
25
|
+
### Generic Project Structure
|
|
26
|
+
```
|
|
27
|
+
project-name/
|
|
28
|
+
├── README.md # Project documentation
|
|
29
|
+
├── .gitignore # Git ignore rules
|
|
30
|
+
├── .env.example # Environment variables template
|
|
31
|
+
├── LICENSE # Project license
|
|
32
|
+
├── CHANGELOG.md # Version history
|
|
33
|
+
├── docs/ # Documentation
|
|
34
|
+
│ ├── API.md
|
|
35
|
+
│ ├── CONTRIBUTING.md
|
|
36
|
+
│ └── DEPLOYMENT.md
|
|
37
|
+
├── src/ # Source code
|
|
38
|
+
│ ├── main/
|
|
39
|
+
│ ├── utils/
|
|
40
|
+
│ └── config/
|
|
41
|
+
├── tests/ # Test files
|
|
42
|
+
│ ├── unit/
|
|
43
|
+
│ ├── integration/
|
|
44
|
+
│ └── fixtures/
|
|
45
|
+
├── scripts/ # Build and deployment scripts
|
|
46
|
+
│ ├── build.sh
|
|
47
|
+
│ ├── deploy.sh
|
|
48
|
+
│ └── setup.sh
|
|
49
|
+
└── config/ # Configuration files
|
|
50
|
+
├── development.yml
|
|
51
|
+
├── production.yml
|
|
52
|
+
└── testing.yml
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Web Application Structure
|
|
56
|
+
```
|
|
57
|
+
web-app/
|
|
58
|
+
├── public/ # Static assets
|
|
59
|
+
│ ├── index.html
|
|
60
|
+
│ ├── favicon.ico
|
|
61
|
+
│ └── assets/
|
|
62
|
+
│ ├── css/
|
|
63
|
+
│ ├── js/
|
|
64
|
+
│ └── images/
|
|
65
|
+
├── src/ # Source code
|
|
66
|
+
│ ├── components/ # Reusable components
|
|
67
|
+
│ ├── pages/ # Page components
|
|
68
|
+
│ ├── services/ # API services
|
|
69
|
+
│ ├── utils/ # Utility functions
|
|
70
|
+
│ ├── styles/ # Stylesheets
|
|
71
|
+
│ └── config/ # Configuration
|
|
72
|
+
├── tests/ # Test files
|
|
73
|
+
└── build/ # Build output
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### API Project Structure
|
|
77
|
+
```
|
|
78
|
+
api-project/
|
|
79
|
+
├── src/
|
|
80
|
+
│ ├── controllers/ # Request handlers
|
|
81
|
+
│ ├── models/ # Data models
|
|
82
|
+
│ ├── services/ # Business logic
|
|
83
|
+
│ ├── middleware/ # Custom middleware
|
|
84
|
+
│ ├── routes/ # API routes
|
|
85
|
+
│ ├── utils/ # Utility functions
|
|
86
|
+
│ └── config/ # Configuration
|
|
87
|
+
├── tests/
|
|
88
|
+
│ ├── unit/
|
|
89
|
+
│ ├── integration/
|
|
90
|
+
│ └── e2e/
|
|
91
|
+
├── docs/
|
|
92
|
+
│ ├── api-spec.yml # OpenAPI specification
|
|
93
|
+
│ └── README.md
|
|
94
|
+
└── scripts/
|
|
95
|
+
├── seed-db.js # Database seeding
|
|
96
|
+
└── migrate.js # Database migrations
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## Essential Configuration Files
|
|
100
|
+
|
|
101
|
+
### .gitignore Template
|
|
102
|
+
```gitignore
|
|
103
|
+
# Dependencies
|
|
104
|
+
node_modules/
|
|
105
|
+
*.log
|
|
106
|
+
npm-debug.log*
|
|
107
|
+
|
|
108
|
+
# Environment files
|
|
109
|
+
.env
|
|
110
|
+
.env.local
|
|
111
|
+
.env.production
|
|
112
|
+
|
|
113
|
+
# Build outputs
|
|
114
|
+
dist/
|
|
115
|
+
build/
|
|
116
|
+
*.tgz
|
|
117
|
+
|
|
118
|
+
# IDE files
|
|
119
|
+
.vscode/
|
|
120
|
+
.idea/
|
|
121
|
+
*.swp
|
|
122
|
+
*.swo
|
|
123
|
+
|
|
124
|
+
# OS files
|
|
125
|
+
.DS_Store
|
|
126
|
+
Thumbs.db
|
|
127
|
+
|
|
128
|
+
# Temporary files
|
|
129
|
+
*.tmp
|
|
130
|
+
*.temp
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### README.md Template
|
|
134
|
+
```markdown
|
|
135
|
+
# Project Name
|
|
136
|
+
|
|
137
|
+
Brief description of what this project does.
|
|
138
|
+
|
|
139
|
+
## Features
|
|
140
|
+
|
|
141
|
+
- Feature 1
|
|
142
|
+
- Feature 2
|
|
143
|
+
- Feature 3
|
|
144
|
+
|
|
145
|
+
## Installation
|
|
146
|
+
|
|
147
|
+
\`\`\`bash
|
|
148
|
+
# Clone the repository
|
|
149
|
+
git clone https://github.com/username/project-name.git
|
|
150
|
+
|
|
151
|
+
# Install dependencies
|
|
152
|
+
npm install
|
|
153
|
+
|
|
154
|
+
# Copy environment file
|
|
155
|
+
cp .env.example .env
|
|
156
|
+
\`\`\`
|
|
157
|
+
|
|
158
|
+
## Usage
|
|
159
|
+
|
|
160
|
+
\`\`\`bash
|
|
161
|
+
# Start development server
|
|
162
|
+
npm run dev
|
|
163
|
+
|
|
164
|
+
# Run tests
|
|
165
|
+
npm test
|
|
166
|
+
|
|
167
|
+
# Build for production
|
|
168
|
+
npm run build
|
|
169
|
+
\`\`\`
|
|
170
|
+
|
|
171
|
+
## API Documentation
|
|
172
|
+
|
|
173
|
+
[API documentation](docs/API.md)
|
|
174
|
+
|
|
175
|
+
## Contributing
|
|
176
|
+
|
|
177
|
+
Please read [CONTRIBUTING.md](docs/CONTRIBUTING.md) for details.
|
|
178
|
+
|
|
179
|
+
## License
|
|
180
|
+
|
|
181
|
+
This project is licensed under the MIT License - see [LICENSE](LICENSE) file.
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### .env.example Template
|
|
185
|
+
```env
|
|
186
|
+
# Application
|
|
187
|
+
NODE_ENV=development
|
|
188
|
+
PORT=3000
|
|
189
|
+
HOST=localhost
|
|
190
|
+
|
|
191
|
+
# Database
|
|
192
|
+
DB_HOST=localhost
|
|
193
|
+
DB_PORT=5432
|
|
194
|
+
DB_NAME=myapp
|
|
195
|
+
DB_USER=username
|
|
196
|
+
DB_PASSWORD=password
|
|
197
|
+
|
|
198
|
+
# API Keys
|
|
199
|
+
API_KEY=your-api-key-here
|
|
200
|
+
SECRET_KEY=your-secret-key-here
|
|
201
|
+
|
|
202
|
+
# External Services
|
|
203
|
+
REDIS_URL=redis://localhost:6379
|
|
204
|
+
EMAIL_SERVICE_API_KEY=your-email-key
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
## Development Environment Setup
|
|
208
|
+
|
|
209
|
+
### Package.json Scripts
|
|
210
|
+
```json
|
|
211
|
+
{
|
|
212
|
+
"scripts": {
|
|
213
|
+
"start": "node src/index.js",
|
|
214
|
+
"dev": "nodemon src/index.js",
|
|
215
|
+
"test": "jest",
|
|
216
|
+
"test:watch": "jest --watch",
|
|
217
|
+
"test:coverage": "jest --coverage",
|
|
218
|
+
"lint": "eslint src/",
|
|
219
|
+
"lint:fix": "eslint src/ --fix",
|
|
220
|
+
"format": "prettier --write src/",
|
|
221
|
+
"build": "webpack --mode production",
|
|
222
|
+
"build:dev": "webpack --mode development",
|
|
223
|
+
"clean": "rm -rf dist/"
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
### Git Initialization
|
|
229
|
+
```bash
|
|
230
|
+
# Initialize Git repository
|
|
231
|
+
git init
|
|
232
|
+
|
|
233
|
+
# Add initial files
|
|
234
|
+
git add .
|
|
235
|
+
|
|
236
|
+
# Create initial commit
|
|
237
|
+
git commit -m "feat: initial project setup
|
|
238
|
+
|
|
239
|
+
- Add project structure
|
|
240
|
+
- Configure development environment
|
|
241
|
+
- Add documentation templates
|
|
242
|
+
- Set up testing framework"
|
|
243
|
+
|
|
244
|
+
# Add remote origin
|
|
245
|
+
git remote add origin https://github.com/username/project-name.git
|
|
246
|
+
|
|
247
|
+
# Push to remote
|
|
248
|
+
git push -u origin main
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
## Language-Specific Setup
|
|
252
|
+
|
|
253
|
+
### Node.js/JavaScript
|
|
254
|
+
```bash
|
|
255
|
+
npm init -y
|
|
256
|
+
npm install --save-dev nodemon jest eslint prettier
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
### Python
|
|
260
|
+
```bash
|
|
261
|
+
python -m venv venv
|
|
262
|
+
source venv/bin/activate # On Windows: venv\Scripts\activate
|
|
263
|
+
pip install -r requirements.txt
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
### Docker Setup
|
|
267
|
+
```dockerfile
|
|
268
|
+
# Dockerfile
|
|
269
|
+
FROM node:16-alpine
|
|
270
|
+
WORKDIR /app
|
|
271
|
+
COPY package*.json ./
|
|
272
|
+
RUN npm install
|
|
273
|
+
COPY . .
|
|
274
|
+
EXPOSE 3000
|
|
275
|
+
CMD ["npm", "start"]
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
```yaml
|
|
279
|
+
# docker-compose.yml
|
|
280
|
+
version: '3.8'
|
|
281
|
+
services:
|
|
282
|
+
app:
|
|
283
|
+
build: .
|
|
284
|
+
ports:
|
|
285
|
+
- "3000:3000"
|
|
286
|
+
environment:
|
|
287
|
+
- NODE_ENV=development
|
|
288
|
+
volumes:
|
|
289
|
+
- .:/app
|
|
290
|
+
- /app/node_modules
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
## Best Practices
|
|
294
|
+
|
|
295
|
+
1. **Consistent Structure** - Follow established conventions
|
|
296
|
+
2. **Clear Documentation** - Write comprehensive README
|
|
297
|
+
3. **Environment Variables** - Keep secrets out of code
|
|
298
|
+
4. **Version Control** - Initialize Git from the start
|
|
299
|
+
5. **Testing Setup** - Configure testing from day one
|
|
300
|
+
6. **Code Quality** - Set up linting and formatting
|
|
301
|
+
7. **CI/CD Ready** - Structure for automated deployment
|
|
302
|
+
8. **Security** - Include security best practices
|
|
303
|
+
|
|
304
|
+
## Checklist
|
|
305
|
+
|
|
306
|
+
- [ ] Create project directory structure
|
|
307
|
+
- [ ] Initialize version control (Git)
|
|
308
|
+
- [ ] Set up package manager (npm, pip, etc.)
|
|
309
|
+
- [ ] Create README.md with project info
|
|
310
|
+
- [ ] Add .gitignore file
|
|
311
|
+
- [ ] Set up environment variables
|
|
312
|
+
- [ ] Configure linting and formatting
|
|
313
|
+
- [ ] Set up testing framework
|
|
314
|
+
- [ ] Create basic documentation
|
|
315
|
+
- [ ] Add license file
|
|
316
|
+
- [ ] Set up CI/CD configuration (if needed)
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
# Route Creator
|
|
2
|
+
|
|
3
|
+
Create API routes and endpoints for Node.js applications with proper structure and best practices.
|
|
4
|
+
|
|
5
|
+
## Purpose
|
|
6
|
+
|
|
7
|
+
This command helps you quickly create new API routes with:
|
|
8
|
+
- RESTful endpoint structure
|
|
9
|
+
- Proper HTTP method handling
|
|
10
|
+
- Input validation
|
|
11
|
+
- Error handling
|
|
12
|
+
- Documentation comments
|
|
13
|
+
- TypeScript support
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
Use this command to create API routes for your Node.js application:
|
|
18
|
+
|
|
19
|
+
```
|
|
20
|
+
/route
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## What this command does
|
|
24
|
+
|
|
25
|
+
1. **Analyzes your project structure** to understand your routing setup
|
|
26
|
+
2. **Asks for route details** (endpoint path, HTTP methods, parameters)
|
|
27
|
+
3. **Creates route files** with proper structure and validation
|
|
28
|
+
4. **Generates TypeScript types** for request/response (if TypeScript project)
|
|
29
|
+
5. **Adds proper error handling** and status codes
|
|
30
|
+
6. **Includes documentation** and examples
|
|
31
|
+
|
|
32
|
+
## Example Output
|
|
33
|
+
|
|
34
|
+
For a `/users` route, this might create:
|
|
35
|
+
|
|
36
|
+
### Express.js Route
|
|
37
|
+
```javascript
|
|
38
|
+
// routes/users.js
|
|
39
|
+
const express = require('express');
|
|
40
|
+
const router = express.Router();
|
|
41
|
+
|
|
42
|
+
// GET /users - Get all users
|
|
43
|
+
router.get('/', async (req, res) => {
|
|
44
|
+
try {
|
|
45
|
+
// TODO: Implement user fetching logic
|
|
46
|
+
const users = [];
|
|
47
|
+
res.json(users);
|
|
48
|
+
} catch (error) {
|
|
49
|
+
res.status(500).json({ error: 'Failed to fetch users' });
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// GET /users/:id - Get user by ID
|
|
54
|
+
router.get('/:id', async (req, res) => {
|
|
55
|
+
try {
|
|
56
|
+
const { id } = req.params;
|
|
57
|
+
// TODO: Implement user fetching by ID
|
|
58
|
+
const user = {};
|
|
59
|
+
|
|
60
|
+
if (!user) {
|
|
61
|
+
return res.status(404).json({ error: 'User not found' });
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
res.json(user);
|
|
65
|
+
} catch (error) {
|
|
66
|
+
res.status(500).json({ error: 'Failed to fetch user' });
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
// POST /users - Create new user
|
|
71
|
+
router.post('/', async (req, res) => {
|
|
72
|
+
try {
|
|
73
|
+
const { name, email } = req.body;
|
|
74
|
+
|
|
75
|
+
// Input validation
|
|
76
|
+
if (!name || !email) {
|
|
77
|
+
return res.status(400).json({
|
|
78
|
+
error: 'Name and email are required'
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// TODO: Implement user creation logic
|
|
83
|
+
const newUser = { id: Date.now(), name, email };
|
|
84
|
+
|
|
85
|
+
res.status(201).json(newUser);
|
|
86
|
+
} catch (error) {
|
|
87
|
+
res.status(500).json({ error: 'Failed to create user' });
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
module.exports = router;
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### TypeScript Route (if TS project)
|
|
95
|
+
```typescript
|
|
96
|
+
// routes/users.ts
|
|
97
|
+
import { Router, Request, Response } from 'express';
|
|
98
|
+
|
|
99
|
+
interface User {
|
|
100
|
+
id: number;
|
|
101
|
+
name: string;
|
|
102
|
+
email: string;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
interface CreateUserRequest {
|
|
106
|
+
name: string;
|
|
107
|
+
email: string;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const router = Router();
|
|
111
|
+
|
|
112
|
+
// GET /users - Get all users
|
|
113
|
+
router.get('/', async (req: Request, res: Response<User[]>) => {
|
|
114
|
+
try {
|
|
115
|
+
// TODO: Implement user fetching logic
|
|
116
|
+
const users: User[] = [];
|
|
117
|
+
res.json(users);
|
|
118
|
+
} catch (error) {
|
|
119
|
+
res.status(500).json({ error: 'Failed to fetch users' } as any);
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
// POST /users - Create new user
|
|
124
|
+
router.post('/', async (req: Request<{}, User, CreateUserRequest>, res: Response<User>) => {
|
|
125
|
+
try {
|
|
126
|
+
const { name, email } = req.body;
|
|
127
|
+
|
|
128
|
+
if (!name || !email) {
|
|
129
|
+
return res.status(400).json({
|
|
130
|
+
error: 'Name and email are required'
|
|
131
|
+
} as any);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// TODO: Implement user creation logic
|
|
135
|
+
const newUser: User = { id: Date.now(), name, email };
|
|
136
|
+
|
|
137
|
+
res.status(201).json(newUser);
|
|
138
|
+
} catch (error) {
|
|
139
|
+
res.status(500).json({ error: 'Failed to create user' } as any);
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
export default router;
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
## When to use this command
|
|
147
|
+
|
|
148
|
+
- **Creating new API endpoints** for your application
|
|
149
|
+
- **Adding CRUD operations** for data models
|
|
150
|
+
- **Building RESTful APIs** with proper structure
|
|
151
|
+
- **Setting up route handlers** with validation and error handling
|
|
152
|
+
- **Generating boilerplate** for API development
|
|
153
|
+
|
|
154
|
+
## Framework Support
|
|
155
|
+
|
|
156
|
+
This command adapts to your specific Node.js framework:
|
|
157
|
+
|
|
158
|
+
### Express.js
|
|
159
|
+
- Creates `routes/` directory structure
|
|
160
|
+
- Uses Express Router
|
|
161
|
+
- Includes middleware support
|
|
162
|
+
- Adds common HTTP methods (GET, POST, PUT, DELETE)
|
|
163
|
+
|
|
164
|
+
### Fastify
|
|
165
|
+
- Creates Fastify route plugins
|
|
166
|
+
- Includes schema validation
|
|
167
|
+
- Uses Fastify's built-in serialization
|
|
168
|
+
- Adds proper error handling
|
|
169
|
+
|
|
170
|
+
### NestJS
|
|
171
|
+
- Creates controller classes
|
|
172
|
+
- Includes decorators (@Get, @Post, etc.)
|
|
173
|
+
- Generates DTOs for validation
|
|
174
|
+
- Uses dependency injection
|
|
175
|
+
|
|
176
|
+
## Best Practices Included
|
|
177
|
+
|
|
178
|
+
- **Input Validation** - Validates request parameters and body
|
|
179
|
+
- **Error Handling** - Proper try/catch blocks and error responses
|
|
180
|
+
- **Status Codes** - Appropriate HTTP status codes for different scenarios
|
|
181
|
+
- **TypeScript Types** - Strong typing for requests and responses
|
|
182
|
+
- **Documentation** - JSDoc comments explaining each endpoint
|
|
183
|
+
- **Security** - Basic input sanitization and validation
|
|
184
|
+
- **Testing Setup** - Suggests test cases for the new routes
|
|
185
|
+
|
|
186
|
+
## Tips
|
|
187
|
+
|
|
188
|
+
1. **Plan your API structure** before creating routes
|
|
189
|
+
2. **Use consistent naming** for endpoints (plural nouns for collections)
|
|
190
|
+
3. **Include proper validation** for all input data
|
|
191
|
+
4. **Add authentication/authorization** where needed
|
|
192
|
+
5. **Test your routes** after creation
|
|
193
|
+
6. **Document your API** for other developers
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
# Django Model Generator
|
|
2
|
+
|
|
3
|
+
Create Django models with proper structure and relationships.
|
|
4
|
+
|
|
5
|
+
## Purpose
|
|
6
|
+
|
|
7
|
+
This command helps you quickly create Django models with fields, relationships, and best practices.
|
|
8
|
+
|
|
9
|
+
## Usage
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
/django-model
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## What this command does
|
|
16
|
+
|
|
17
|
+
1. **Creates model classes** with proper field definitions
|
|
18
|
+
2. **Adds relationships** (ForeignKey, ManyToMany, OneToOne)
|
|
19
|
+
3. **Includes meta options** and model methods
|
|
20
|
+
4. **Generates migrations** automatically
|
|
21
|
+
5. **Follows Django conventions** and best practices
|
|
22
|
+
|
|
23
|
+
## Example Output
|
|
24
|
+
|
|
25
|
+
```python
|
|
26
|
+
# models.py
|
|
27
|
+
from django.db import models
|
|
28
|
+
from django.contrib.auth.models import User
|
|
29
|
+
|
|
30
|
+
class Category(models.Model):
|
|
31
|
+
name = models.CharField(max_length=100, unique=True)
|
|
32
|
+
slug = models.SlugField(unique=True)
|
|
33
|
+
description = models.TextField(blank=True)
|
|
34
|
+
created_at = models.DateTimeField(auto_now_add=True)
|
|
35
|
+
updated_at = models.DateTimeField(auto_now=True)
|
|
36
|
+
|
|
37
|
+
class Meta:
|
|
38
|
+
verbose_name_plural = "categories"
|
|
39
|
+
ordering = ['name']
|
|
40
|
+
|
|
41
|
+
def __str__(self):
|
|
42
|
+
return self.name
|
|
43
|
+
|
|
44
|
+
class Post(models.Model):
|
|
45
|
+
STATUS_CHOICES = [
|
|
46
|
+
('draft', 'Draft'),
|
|
47
|
+
('published', 'Published'),
|
|
48
|
+
('archived', 'Archived'),
|
|
49
|
+
]
|
|
50
|
+
|
|
51
|
+
title = models.CharField(max_length=200)
|
|
52
|
+
slug = models.SlugField(unique=True)
|
|
53
|
+
content = models.TextField()
|
|
54
|
+
status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='draft')
|
|
55
|
+
author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='posts')
|
|
56
|
+
category = models.ForeignKey(Category, on_delete=models.SET_NULL, null=True, blank=True)
|
|
57
|
+
tags = models.ManyToManyField('Tag', blank=True)
|
|
58
|
+
created_at = models.DateTimeField(auto_now_add=True)
|
|
59
|
+
updated_at = models.DateTimeField(auto_now=True)
|
|
60
|
+
|
|
61
|
+
class Meta:
|
|
62
|
+
ordering = ['-created_at']
|
|
63
|
+
verbose_name = 'blog post'
|
|
64
|
+
verbose_name_plural = 'blog posts'
|
|
65
|
+
|
|
66
|
+
def __str__(self):
|
|
67
|
+
return self.title
|
|
68
|
+
|
|
69
|
+
def get_absolute_url(self):
|
|
70
|
+
return reverse('post_detail', kwargs={'slug': self.slug})
|
|
71
|
+
|
|
72
|
+
class Tag(models.Model):
|
|
73
|
+
name = models.CharField(max_length=50, unique=True)
|
|
74
|
+
slug = models.SlugField(unique=True)
|
|
75
|
+
|
|
76
|
+
def __str__(self):
|
|
77
|
+
return self.name
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## Field Types Supported
|
|
81
|
+
|
|
82
|
+
- **CharField** - Text fields with max length
|
|
83
|
+
- **TextField** - Large text fields
|
|
84
|
+
- **IntegerField** - Integer numbers
|
|
85
|
+
- **FloatField** - Floating point numbers
|
|
86
|
+
- **BooleanField** - True/False values
|
|
87
|
+
- **DateField** - Date only
|
|
88
|
+
- **DateTimeField** - Date and time
|
|
89
|
+
- **EmailField** - Email addresses
|
|
90
|
+
- **URLField** - URLs
|
|
91
|
+
- **ImageField** - Image uploads
|
|
92
|
+
- **FileField** - File uploads
|
|
93
|
+
- **JSONField** - JSON data (PostgreSQL)
|
|
94
|
+
|
|
95
|
+
## Relationships
|
|
96
|
+
|
|
97
|
+
- **ForeignKey** - One-to-many relationships
|
|
98
|
+
- **ManyToManyField** - Many-to-many relationships
|
|
99
|
+
- **OneToOneField** - One-to-one relationships
|
|
100
|
+
|
|
101
|
+
## Best Practices Included
|
|
102
|
+
|
|
103
|
+
- Proper field choices and defaults
|
|
104
|
+
- Appropriate related_name attributes
|
|
105
|
+
- __str__ methods for admin interface
|
|
106
|
+
- Meta class with ordering and verbose names
|
|
107
|
+
- get_absolute_url methods where appropriate
|
|
108
|
+
- Proper use of null and blank parameters
|
|
109
|
+
- Field validation and constraints
|
|
110
|
+
|
|
111
|
+
## After Creating Models
|
|
112
|
+
|
|
113
|
+
```bash
|
|
114
|
+
# Create and apply migrations
|
|
115
|
+
python manage.py makemigrations
|
|
116
|
+
python manage.py migrate
|
|
117
|
+
|
|
118
|
+
# Register in admin (optional)
|
|
119
|
+
# Add to admin.py:
|
|
120
|
+
from django.contrib import admin
|
|
121
|
+
from .models import Post, Category, Tag
|
|
122
|
+
|
|
123
|
+
admin.site.register([Post, Category, Tag])
|
|
124
|
+
```
|