File tree Expand file tree Collapse file tree 3 files changed +35
-0
lines changed
Expand file tree Collapse file tree 3 files changed +35
-0
lines changed Original file line number Diff line number Diff line change 1+ import os
2+
3+ from django .core .management import call_command
4+ from django .core .management .base import BaseCommand
5+
6+
7+ class Command (BaseCommand ):
8+ help = "Dump data for quiz models (Question and Topic) into a JSON file."
9+
10+ def add_arguments (self , parser ):
11+ parser .add_argument (
12+ "--output" ,
13+ type = str ,
14+ default = "./data/quiz_data.json" ,
15+ help = "Output file path (default: ./data/quiz_data.json)" ,
16+ )
17+
18+ def handle (self , * args , ** kwargs ):
19+ output_path = kwargs ["output" ]
20+ os .makedirs (os .path .dirname (output_path ), exist_ok = True )
21+
22+ self .stdout .write ("Dumping quiz data into: {}" .format (output_path ))
23+
24+ call_command (
25+ "dumpdata" ,
26+ "quiz.Question" ,
27+ "quiz.Topic" ,
28+ "--natural-primary" ,
29+ "--natural-foreign" ,
30+ "--indent" ,
31+ "4" ,
32+ stdout = open (output_path , "w" ),
33+ )
34+
35+ self .stdout .write (self .style .SUCCESS ("Data dumped successfully!" ))
You can’t perform that action at this time.
0 commit comments