← Back to Django Course | Chapter 8: Django Admin | Lesson 3 of 8

Admin में Models Register करना

एक model register करना Django को बताता है 'इसे भी admin dashboard में दिखाओ'।
Syntax
markup
from django.contrib import admin
from .models import ModelName

admin.site.register(ModelName)

सबसे Simple Registration

एक model class के साथ admin.site.register() call करना इसके लिए Django के defaults उपयोग करते हुए एक पूरी तरह working list/add/edit/delete interface पाने के लिए काफी है।

उदाहरण: The Simplest Registration

This single line makes Book manageable from the admin dashboard.

markup
from django.contrib import admin
from .models import Book

admin.site.register(Book)
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

कई Models Register करना

आप एक ही admin.py file में एक ही app से कई models register कर सकते हैं, हर एक की अपनी call के साथ।

उदाहरण: Registering Multiple Models

Both Book and Author now appear as separate sections in the admin dashboard.

markup
from django.contrib import admin
from .models import Book, Author

admin.site.register(Book)
admin.site.register(Author)
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

`@admin.register` Decorator उपयोग करना

register() अलग से call करने के बजाय, आप एक ModelAdmin class को @admin.register(Model) से decorate कर सकते हैं -- एक बार आप admin customize करना शुरू करें तो यह useful है, बाद के topics में covered है।

Warning: एक ही model को दो बार register करना (जैसे एक बार decorator से और एक बार admin.site.register से) django.contrib.admin.sites.AlreadyRegistered raise करता है।

उदाहरण: Using the @admin.register Decorator

This registers Book using an empty ModelAdmin, equivalent to the plain register() call but ready for customization.

markup
from django.contrib import admin
from .models import Book

@admin.register(Book)
class BookAdmin(admin.ModelAdmin):
    pass
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक ही model को दो बार register करना, जो एक AlreadyRegistered error raise करता है।
  2. एक model को गलत app के admin.py में register करना जहां Django इसे कभी import नहीं करता।
  3. यह भूल जाना कि app को इसके admin.py को बिल्कुल load होने के लिए भी INSTALLED_APPS में listed होना चाहिए।
चैप्टर सारांश
  • admin.site.register(Model) dashboard में एक model expose करने का सबसे simple तरीका है।
  • @admin.register(Model) decorator वही काम करता है जबकि एक ही step में एक ModelAdmin class define करता है।
  • Registration हमेशा owning app की admin.py file में होता है।
  • हर INSTALLED_APPS app की admin.py Django द्वारा startup पर auto-discovered और load होती है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.