Admin में Models Register करना
In this page:
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.
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.
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 है।
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.
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. #}
- एक ही model को दो बार register करना, जो एक
AlreadyRegisterederror raise करता है। - एक model को गलत app के
admin.pyमें register करना जहां Django इसे कभी import नहीं करता। - यह भूल जाना कि app को इसके
admin.pyको बिल्कुल load होने के लिए भीINSTALLED_APPSमें listed होना चाहिए।
admin.site.register(Model)dashboard में एक model expose करने का सबसे simple तरीका है।@admin.register(Model)decorator वही काम करता है जबकि एक ही step में एकModelAdminclass define करता है।- Registration हमेशा owning app की
admin.pyfile में होता है। - हर
INSTALLED_APPSapp कीadmin.pyDjango द्वारा startup पर auto-discovered और load होती है।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: