{"id":21351885,"url":"https://github.com/hygull/try-django-admin-cookbook","last_synced_at":"2025-03-16T04:41:30.132Z","repository":{"id":80469012,"uuid":"186505184","full_name":"hygull/try-django-admin-cookbook","owner":"hygull","description":"A Django project which deals with customizing Django Admin Interface. It is impressed from https://books.agiliq.com/projects/django-admin-cookbook/en/latest/ and uses similar/different kind of examples. ","archived":false,"fork":false,"pushed_at":"2019-05-18T11:53:17.000Z","size":38,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-22T17:13:41.853Z","etag":null,"topics":["admin-interface","birthday","birthday-reminder","cookbook","customization","django-2-2","django2","python3-6-7","python36"],"latest_commit_sha":null,"homepage":"https://hygull.github.io/try-django-admin-cookbook/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hygull.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-05-13T22:36:52.000Z","updated_at":"2023-07-19T11:52:59.000Z","dependencies_parsed_at":null,"dependency_job_id":"6d8c71e2-c2c6-4eb4-8282-a3aa90d94701","html_url":"https://github.com/hygull/try-django-admin-cookbook","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hygull%2Ftry-django-admin-cookbook","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hygull%2Ftry-django-admin-cookbook/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hygull%2Ftry-django-admin-cookbook/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hygull%2Ftry-django-admin-cookbook/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hygull","download_url":"https://codeload.github.com/hygull/try-django-admin-cookbook/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243826783,"owners_count":20354220,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["admin-interface","birthday","birthday-reminder","cookbook","customization","django-2-2","django2","python3-6-7","python36"],"created_at":"2024-11-22T03:11:43.847Z","updated_at":"2025-03-16T04:41:30.106Z","avatar_url":"https://github.com/hygull.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# try-django-admin-cookbook\n\nA Django project which deals with customizing Django Admin Interface. It is impressed from https://books.agiliq.com/projects/django-admin-cookbook/en/latest/ and uses similar/different kind of examples. \n\n\u003e To read colourful documentation visit [https://hygull.github.io/try-django-admin-cookbook/](https://hygull.github.io/try-django-admin-cookbook/). \n\u003e\n\u003eThe purpose of this project is to learn by making mistakes so don't think about model's structure first time as later it has been enhanced. \n\u003e\n\u003ePlease focus on how it flows and changes as we advance.\n\n+ First have a look at this docs first.\n\n\t+ [1.md](./docs/command_history/1.md)\n\n\t+ [2.md](./docs/command_history/2.md)\n\n\t+ [3.md](./docs/command_history/3.md)\n\n\t+ [4.md](./docs/command_history/4.md)\n\n\t+ [5.md](./docs/command_history/5.md)\n\n    + [6.md](./docs/command_history/6.md)\n\n## Defining models, applicaton registry, changing default site texts\n\n+ In `cookbook/urls.py`, add the following lines just after import statements\n\n```python\n# Admin Interface: change site-header, site-title \u0026 index-title \nadmin.site.site_header = \"Admin Interface Cookbook\"\nadmin.site.site_title = \"Customizing Admin Interface\"\nadmin.site.index_title = \"Apps and related models\"\n```\n\nso that it will look like below.\n\n\u003e **Note:** I am not going to paste the whole code next time, this is to make sure my point is clear to you readers only first time. So stay active, stay focused.\n\n```python\n\"\"\"cookbook URL Configuration\n\nThe `urlpatterns` list routes URLs to views. For more information please see:\n    https://docs.djangoproject.com/en/2.2/topics/http/urls/\nExamples:\nFunction views\n    1. Add an import:  from my_app import views\n    2. Add a URL to urlpatterns:  path('', views.home, name='home')\nClass-based views\n    1. Add an import:  from other_app.views import Home\n    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')\nIncluding another URLconf\n    1. Import the include() function: from django.urls import include, path\n    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))\n\"\"\"\nfrom django.contrib import admin\nfrom django.urls import path\n\n# Admin Interface: change site-header, site-title \u0026 index-title \nadmin.site.site_header = \"Admin Interface Cookbook\"\nadmin.site.site_title = \"Customizing Admin Interface\"\nadmin.site.index_title = \"Apps and related models\"\n\nurlpatterns = [\n    path('admin/', admin.site.urls),\n]\n```\n\n+ Now create 2 apps named **users** and **posts**.\n\n+ Define the following 2 models in **users/models.py**.\n\n```python\n\nclass Address(models.Model):\n    city = models.CharField(max_length=50, default='', help_text=\"User's village name\")\n    village = models.CharField(max_length=50, default='', help_text=\"User's city name\")\n    district = models.CharField(max_length=50, null=True, help_text=\"User's district name\")\n    state = models.CharField(max_length=50, blank=True, help_text=\"User's state name\")\n\n    def __str__(self):\n        return \"Address - {0}\".format(self.pk)\n\n\nclass User(models.Model):\n    first_name = models.CharField(max_length=50, null=False, blank=False, help_text=\"User's first name\")\n    last_name = models.CharField(max_length=50, null=False, blank=False, help_text=\"User's last name\")\n    date_of_birth = models.DateField(help_text=\"User's date of birth\")\n    address = models.ForeignKey(Address, on_delete=models.CASCADE, help_text=\"User's address\")\n    created_at = models.DateTimeField(auto_now_add=True, help_text=\"User created at\")\n    updated_at = models.DateTimeField(auto_now=True, help_text=\"User details last updated at\")\n    active = models.BooleanField(default=True, help_text=\"Active status\")\n\n    def __str__(self):\n        return \"User {0}\".format(self.pk)\n\n```\n\n+ Now, define the following 2 models(including 1 import statement) in **posts/models.py**.\n\n```python\nfrom users.models import User\n\nclass Category(models.Model):\n    name = models.CharField(max_length=50, help_text=\"Name of category\")\n    description = models.TextField(default='', help_text=\"Description of category\")\n\n    def __str__(self):\n        return \"Category - {0}\".format(self.pk)\n\n\nclass Post(models.Model):\n    title = models.CharField(max_length=50, default='', help_text=\"Post's title\")\n    description = models.TextField(help_text=\"Post's description\")\n    posted_by = models.ForeignKey(User, on_delete=models.CASCADE, help_text=\"Who posted?\")\n    created_at = models.DateTimeField(auto_now_add=True, help_text=\"Post created at\")\n    updated_at = models.DateTimeField(auto_now=True, help_text=\"Post last updated at\")\n    category = models.ForeignKey(Category, on_delete=models.CASCADE, help_text=\"Post belongs to\")\n    active = models.BooleanField(default=True, help_text=\"Active status\")\n\n    def __str__(self):\n        return \"Post - {0} by {1}\".format(self.pk, self.posted_by.first_name)\n\n```\n\n+ Now, **makemigrations** \u0026 **migrate**.\n\n+ Add following to **users/admin.py**.\n\n```python\nfrom users.models import User, Address\n\n@admin.register(User)\nclass UserAdmin(admin.ModelAdmin):\n    list_display = [\n        \"pk\",\n        \"first_name\",\n        \"last_name\",\n        \"date_of_birth\",\n        \"created_at\",\n        \"updated_at\",\n        \"active\"\n    ]\n    search_fields = [\n        \"pk\",\n        \"first_name\",\n        \"last_name\"\n    ]\n\n@admin.register(Address)\nclass AddressAdmin(admin.ModelAdmin):\n    search_fields = [\n        \"pk\",\n        \"city\",\n        \"village\",\n        \"district\",\n        \"state\"\n    ]\n```\n\n+ Now add following to **posts/admin.py**.\n\n```python\nfrom posts.models import Post, Category\n\n@admin.register(Post)\nclass PostAdmin(admin.ModelAdmin):\n    list_display = [\n        \"pk\",\n        \"title\",\n        \"active\",\n        \"posted_by\",\n        \"created_at\",\n        \"updated_at\"\n    ]\n\n    search_fields = [\n        \"pk\",\n        \"title\",\n        \"active\"\n    ]\n\n\n@admin.register(Category)\nclass CategoryAdmin(admin.ModelAdmin):\n    search_fields = [\n        \"pk\",\n        \"name\"\n    ]\n```\n\n+ Runserver `python manage.py runserver` and check. Now you can see your apps in admin interface.\n\n+ You will be seeing **Categorys** in the page, so just update the Category model by adding Meta class. And it will change **Categorys** to **Categories**.\n\n```python\nclass Category(models.Model):\n    name = models.CharField(max_length=50, help_text=\"Name of category\")\n    description = models.TextField(default='', help_text=\"Description of category\")\n\n    def __str__(self):\n        return \"Category - {0}\".format(self.pk)\n\n    class Meta:\n        verbose_name_plural = \"Categories\"\n```\n\n### Two separate admin sites (for users \u0026 posts)\n\n+ Append the following code in **users/admin.py** (very bottom).\n\n```python\n# *---* Separate Admin site *---*\nclass UserAdminSite(admin.AdminSite):\n    site_header = \"User Admin Site\"\n    site_title = \"Users\"\n    index_title = \"Users list\"\n\nusers_admin_site = UserAdminSite(name=\"users-admin-site\")\nusers_admin_site.register(User)\nusers_admin_site.register(Address)\n\n```\n\n+ Now, append the following code in **posts/admin.py** (very bottom).\n\n```python\n# *---* Separate Admin site *---*\nclass PostAdminSite(admin.AdminSite):\n    site_header = \"Post Admin Site\"\n    site_title = \"Posts\"\n    index_title = \"Posts list\"\n\nposts_admin_site = PostAdminSite(name=\"posts-admin-site\")\nposts_admin_site.register(Post)\nposts_admin_site.register(Category)\n\n```\n\n+ Open **cookbook/urls.py**, add first 2 import at very top \n\n```python\nfrom users.admin import users_admin_site\nfrom posts.admin import posts_admin_site\n```\n\n+ And then only change **urlpatterns** to\n\n```python\nurlpatterns = [\n    path('admin/', admin.site.urls),\n\n    # After defining the following 2 patterns, you won't be able to access \n    # default home(/) page. Need to design our own custom home(/) page\n    path('users-admin/', users_admin_site.urls),\n    path('posts-admin/', posts_admin_site.urls),\n]\n\n```\n\n+ Now, it's time to hide **User** \u0026 **Group** (from `django.contrib.auth` app) from admin interface. Add the below import statement after the import statements that we added in previous step.\n\n```python\nfrom django.contrib.auth.models import User, Group\n```\n\n+ Then add the following statements just before **urlpatterns** list.\n\n```python\n# Hide User \u0026 Group from admin (/admin/)\nadmin.site.unregister(User)\nadmin.site.unregister(Group)\n```\n\n\n# References \n\n+ https://books.agiliq.com/en/latest/\n\n+ [https://books.agiliq.com/projects/django-admin-cookbook/en/latest/](https://books.agiliq.com/projects/django-admin-cookbook/en/latest/)\n\n+ [Change site header, site title, index title](https://books.agiliq.com/projects/django-admin-cookbook/en/latest/change_text.html)\n\n+ [Set plural text for model](https://books.agiliq.com/projects/django-admin-cookbook/en/latest/plural_text.html)\n\n+ [Create 2 independent admin sites](https://books.agiliq.com/projects/django-admin-cookbook/en/latest/two_admin.html)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhygull%2Ftry-django-admin-cookbook","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhygull%2Ftry-django-admin-cookbook","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhygull%2Ftry-django-admin-cookbook/lists"}