@@ -61,11 +61,89 @@ Custom media files can be included using the Media class.
6161
6262## Usage
6363
64- - Place the code in the ` admin.py ` file of your Django application.
65- - Adjust the configuration constants as needed.
66- - Define ` admin_meta ` in your models to customize the admin interface.
64+ 1 . ** Place the code in the ` admin.py ` file of your Django application.**
65+ 2 . ** Adjust the configuration constants as needed.**
66+ 3 . ** Define ` admin_meta ` in your models to customize the admin interface.**
6767
68- ## Example Model Configuration
68+ ## Example Model Configuration 🛠️
69+
70+ Here's an example of how you can define your models to leverage the dynamic admin interface:
71+
72+ ``` python
73+ class BlogCategory (CommonModel ):
74+ category = models.CharField (max_length = 100 , unique = True )
75+ slug = models.SlugField (max_length = 100 , unique = True )
76+ image = models.FileField (blank = True ,null = True ,upload_to = ' blog_category/' )
77+
78+ def __str__ (self ):
79+ return str (self .category)
80+
81+ class Blog (CommonModel ):
82+
83+ head_default= ''' <meta name="title" content=" ">
84+ <meta name="description" content=" ">
85+ <meta name="keywords" content=" ">
86+ <meta name="robots" content="index, follow">'''
87+
88+ title = models.CharField (max_length = 200 )
89+ sub_title = models.CharField (max_length = 200 , blank = True ,null = True )
90+ thumbnail = models.ImageField (upload_to = " blog/" )
91+ category = models.ForeignKey (BlogCategory, null = True , on_delete = models.SET_NULL )
92+ featured_text = HTMLField (null = True , blank = True )
93+ text = HTMLField (null = True , blank = True )
94+ slug = models.SlugField (unique = True )
95+ readtime = models.CharField (max_length = 200 ,null = True , blank = True )
96+ tags = models.TextField (null = True , blank = True , default = ' all' )
97+ head = models.TextField (null = True , blank = True , default = head_default)
98+
99+ order_by = models.IntegerField (default = 0 )
100+
101+ created_at = models.DateTimeField (auto_now_add = True , blank = True , null = True )
102+ updated_at = models.DateTimeField (auto_now = True , blank = True , null = True )
103+ created_by = models.CharField (max_length = 300 )
104+
105+ admin_meta = {
106+ ' list_display' : (" __str__" ," category" ," created_at" ," updated_at" ),
107+ ' list_editable' : (" category" ,),
108+ ' list_per_page' : 50 ,
109+ ' list_filter' : (" category" ,),
110+ ' inline' : [
111+ {' BlogImage' : ' blog' }
112+ ]
113+ }
114+
115+ def __str__ (self ):
116+ return str (self .title)
117+
118+ class Meta :
119+ verbose_name_plural = " Blog"
120+ ordering = [' order_by' ] # Sort in desc order
121+
122+ class BlogImage (CommonModel ):
123+ blog = models.ForeignKey (Blog, on_delete = models.CASCADE )
124+ image = models.ImageField (upload_to = " blog_images/" )
125+ order_by = models.IntegerField (default = 0 )
126+
127+ def __str__ (self ):
128+ return str (self .blog)
129+
130+ class Meta :
131+ verbose_name_plural = " Blog Image"
132+ ordering = [' order_by' ] # Sort in desc order
133+ ```
134+
135+ ### Using ` admin_meta ` Dictionary
136+
137+ You can control the admin interface directly from your ` models.py ` using the ` admin_meta ` dictionary. Here are some key attributes you can define:
138+
139+ - ** ` list_display ` ** : Specify fields to be displayed in the list view.
140+ - ** ` list_editable ` ** : Fields that can be edited directly in the list view.
141+ - ** ` list_filter ` ** : Fields to filter the list view.
142+ - ** ` inline ` ** : Define inline models to be displayed.
143+
144+ ### Example with JSONField Schema
145+
146+ If you have a JSONField in your model, you can customize its form field using a schema:
69147
70148``` python
71149class MyModel (models .Model ):
@@ -100,6 +178,40 @@ class MyModel(models.Model):
100178 pass
101179```
102180
103- ## Note
181+ ### JSON Editor Widget
182+
183+ The ` JsonEditorWidget ` class is used to render JSON fields with a user-friendly JSON editor:
184+
185+ ``` python
186+ class JsonEditorWidget (widgets .Widget ):
187+ template_name = ' json_editor_widget.html'
188+
189+ def __init__ (self , schema , * args , ** kwargs ):
190+ super ().__init__ (* args, ** kwargs)
191+ self .schema = schema
192+
193+ def render (self , name , value , attrs = None , renderer = None ):
194+ json_value = value if value else ' {} '
195+ return mark_safe(f '''
196+ <!-- JSON Editor Code -->
197+ <textarea name=" { name} " id="tx_ { attrs[' id' ]} " style="display:none;"> { json_value} </textarea>
198+ <div id="editor_ { attrs[' id' ]} "></div>
199+ <script>
200+ document.addEventListener("DOMContentLoaded", function() {{
201+ var editor = new JSONEditor(document.getElementById("editor_ { attrs[' id' ]} "), {{
202+ schema: { self .schema} ,
203+ startval: { json_value} ,
204+ theme: 'html',
205+ iconlib: 'fontawesome5',
206+ }} );
207+ editor.on('change', function() {{
208+ document.getElementById("tx_ { attrs[' id' ]} ").value = JSON.stringify(editor.getValue());
209+ }} );
210+ }} );
211+ </script>
212+ ''' )
213+ ```
214+
215+ ## Note 📌
104216
105217Make sure to replace ` 'web' ` with your actual app name in the ` global_app_name ` variable.
0 commit comments