Skip to content

Commit 009d707

Browse files
committed
Provided Django ORM implementation: #16
1 parent dbeef3f commit 009d707

2 files changed

Lines changed: 134 additions & 11 deletions

File tree

README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,62 @@ class GroupAdmin(TortoiseModelAdmin):
190190
search_fields = ("name",)
191191
```
192192

193+
#### For Django ORM:
194+
195+
```python
196+
from asgiref.sync import sync_to_async
197+
from django.db import models
198+
199+
from fastadmin import DjangoModelAdmin, register
200+
201+
202+
203+
class User(models.Model):
204+
username = fields.CharField(max_length=255, unique=True)
205+
hash_password = fields.CharField(max_length=255)
206+
is_superuser = fields.BooleanField(default=False)
207+
is_active = fields.BooleanField(default=False)
208+
209+
...
210+
211+
212+
213+
class Group(models.Model):
214+
name = fields.CharField(max_length=255)
215+
...
216+
217+
218+
@register(User)
219+
class UserAdmin(DjangoModelAdmin):
220+
label_fields = ("username",)
221+
exclude = ("hash_password",)
222+
list_display = ("id", "username", "is_superuser", "is_active")
223+
list_display_links = ("id", "username")
224+
list_filter = ("id", "username", "is_superuser", "is_active")
225+
search_fields = ("username",)
226+
227+
@sync_to_async
228+
def _authenticate(self, username, password):
229+
obj = User.objects.filter(username=username, is_superuser=True).first()
230+
if not obj:
231+
return None
232+
if not obj.check_password(password):
233+
return None
234+
return obj.id
235+
236+
async def authenticate(self, username, password):
237+
return await self._authenticate(username, password)
238+
239+
240+
@register(Group)
241+
class GroupAdmin(DjangoModelAdmin):
242+
label_fields = ("name",)
243+
list_display = ("id", "name")
244+
list_display_links = ("id",)
245+
list_filter = ("id", "name")
246+
search_fields = ("name",)
247+
```
248+
193249
#### For SQLAlchemy:
194250

195251
Coming soon...

docs/index.html

Lines changed: 78 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@ <h4 class="title">FastAdmin</h4>
181181
<li class="nav-item">
182182
<a class="nav-link" href="#changelog">Changelog</a>
183183
<ul class="nav flex-column">
184+
<li class="nav-item">
185+
<a class="nav-link" href="#v0_1_21">v0.1.21</a>
186+
</li>
184187
<li class="nav-item">
185188
<a class="nav-link" href="#v0_1_20">v0.1.20</a>
186189
</li>
@@ -203,7 +206,7 @@ <h1>Documentation</h1>
203206
<div class="row">
204207
<div class="col-sm-6 col-lg-4">
205208
<ul class="list-unstyled">
206-
<li><strong>Version:</strong> 1.1.18</li>
209+
<li><strong>Version:</strong> 1.1.21</li>
207210
<li>
208211
<strong>Author:</strong>
209212
<a href="mailto:vsdudakov@gmail.com" target="_blank"
@@ -356,7 +359,7 @@ <h4>Django Framework</h4>
356359
See <a href="#settings">section</a>.
357360
</p>
358361
<p class="lead">Create and register first AdminModel classes</p>
359-
<h5>Tortoise ORM</h5>
362+
<h4>Tortoise ORM</h4>
360363
<pre>
361364
<code class="language-python">
362365
import bcrypt
@@ -405,6 +408,67 @@ <h5>Tortoise ORM</h5>
405408
search_fields = ("name",)
406409
</code>
407410
</pre>
411+
<h4>Django ORM</h4>
412+
<pre>
413+
<code class="language-python">
414+
from asgiref.sync import sync_to_async
415+
from django.db import models
416+
417+
from fastadmin import DjangoModelAdmin, register
418+
419+
420+
421+
class User(models.Model):
422+
username = fields.CharField(max_length=255, unique=True)
423+
hash_password = fields.CharField(max_length=255)
424+
is_superuser = fields.BooleanField(default=False)
425+
is_active = fields.BooleanField(default=False)
426+
427+
...
428+
429+
430+
431+
class Group(models.Model):
432+
name = fields.CharField(max_length=255)
433+
...
434+
435+
436+
@register(User)
437+
class UserAdmin(DjangoModelAdmin):
438+
label_fields = ("username",)
439+
exclude = ("hash_password",)
440+
list_display = ("id", "username", "is_superuser", "is_active")
441+
list_display_links = ("id", "username")
442+
list_filter = ("id", "username", "is_superuser", "is_active")
443+
search_fields = ("username",)
444+
445+
@sync_to_async
446+
def _authenticate(self, username, password):
447+
obj = User.objects.filter(username=username, is_superuser=True).first()
448+
if not obj:
449+
return None
450+
if not obj.check_password(password):
451+
return None
452+
return obj.id
453+
454+
async def authenticate(self, username, password):
455+
return await self._authenticate(username, password)
456+
457+
458+
@register(Group)
459+
class GroupAdmin(DjangoModelAdmin):
460+
label_fields = ("name",)
461+
list_display = ("id", "name")
462+
list_display_links = ("id",)
463+
list_filter = ("id", "name")
464+
search_fields = ("name",)
465+
</code>
466+
</pre>
467+
<h4>SQLAlchemy ORM</h4>
468+
<p class="alert alert-info">Coming soon...</p>
469+
<h4>Pony ORM</h4>
470+
<p class="alert alert-info">Coming soon...</p>
471+
408472
<p class="alert alert-warning">
409473
Note: You have to implement authenticate method for FastAdmin
410474
authentication on AdminModel class which is registered for
@@ -416,11 +480,6 @@ <h5>Tortoise ORM</h5>
416480
<a href="#inline_model_admin_objects">InlineModelAdmin</a>
417481
sections.
418482
</p>
419-
420-
<h5>SQLAlchemy ORM</h5>
421-
<p class="alert alert-info">Coming soon...</p>
422-
<h5>Pony ORM</h5>
423-
<p class="alert alert-info">Coming soon...</p>
424483
</section>
425484
<hr class="divider" />
426485

@@ -1040,21 +1099,29 @@ <h2>Changelog</h2>
10401099
See what's new added, changed, fixed, improved or updated in the
10411100
latest versions.
10421101
</p>
1043-
<hr class="divider">
1102+
<hr class="small-divider">
10441103
<!-- <p class="alert alert-info mb-5"> For Future Updates Follow Us <a target="_blank" href="http://themeforest.net/user/harnishdesign?ref=HarnishDesign">@themeforest</a> / <a target="_blank" href="http://facebook.com/harnishdesign">@facebook</a> / <a target="_blank" href="http://twitter.com/harnishdesign">@twitter</a> / <a target="_blank" href="https://dribbble.com/harnishdesign">@Dribbble</a></p> -->
10451104

1105+
<h3 id="v0_1_21">Version 0.1.21 <small class="text-muted">(13 March, 2023)</small></h3>
1106+
<ul class="changelog">
1107+
<li>Added support for Django ORM.</li>
1108+
<li>Added modular structure for bakend unit tests.</li>
1109+
</ul>
1110+
<hr class="small-divider">
1111+
10461112
<h3 id="v0_1_20">Version 0.1.20 <small class="text-muted">(11 March, 2023)</small></h3>
10471113
<ul class="changelog">
1048-
<li>Added supporting Django, Flask + moduling architecture.</li>
1114+
<li>Added support for Django, Flask frameworks.</li>
1115+
<li>Added modular architecture for backend API.</li>
10491116
<li>Fixed some minor bugs.</li>
10501117
</ul>
1051-
<hr class="divider">
1118+
<hr class="small-divider">
10521119

10531120
<h3 id="v0_1_19">Version 0.1.19 <small class="text-muted">(9 March, 2023)</small></h3>
10541121
<ul class="changelog">
10551122
<li>Fixed some minor bugs.</li>
10561123
</ul>
1057-
<hr class="divider">
1124+
<hr class="small-divider">
10581125

10591126
<h3 id="v0_1_18">Version 0.1.18 <small class="text-muted">(7 March, 2023)</small></h3>
10601127
<ul class="changelog">

0 commit comments

Comments
 (0)