-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
54 additions
and
1 deletion.
There are no files selected for viewing
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,43 @@ | ||
How to add a database view to Django admin? | ||
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | ||
|
||
You have a database view, created as this:: | ||
|
||
create view entities_entity as | ||
select id, name from entities_hero | ||
union | ||
select 10000+id as id, name from entities_villain | ||
|
||
|
||
It has all the names from :code:`Hero` and :code:`Villain`. The id for Villain ae set :code:`10000+id as id` | ||
because we don't indent to cross 10000 Heroes:: | ||
|
||
sqlite> select * from entities_entity; | ||
1|Krishna | ||
2|Vishnu | ||
3|Achilles | ||
4|Thor | ||
5|Zeus | ||
6|Athena | ||
7|Apollo | ||
10001|Ravana | ||
10002|Fenrir | ||
|
||
Then you add a :code:`managed=False` model:: | ||
|
||
class AllEntity(models.Model): | ||
name = models.CharField(max_length=100) | ||
|
||
class Meta: | ||
managed = False | ||
db_table = "entities_entity" | ||
|
||
And add it to admin.:: | ||
|
||
@admin.register(AllEntity) | ||
class AllEntiryAdmin(admin.ModelAdmin): | ||
list_display = ("id", "name") | ||
|
||
And your admin looks like this | ||
|
||
.. image:: database_view.png |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters