Ways to Register Models to the Admin (मॉडल्स को एडमिन में रजिस्टर करने के तरीके)

Ways to Register Models to the Admin (मॉडल्स को एडमिन में रजिस्टर करने के तरीके)


1. Simple Registration (सरल पंजीकरण) 

 The easiest way to register a model in Django. Just import the model and register it using admin.site.register . Django में मॉडल को रजिस्टर करने का सबसे आसान तरीका। बस मॉडल को इम्पोर्ट करें और `admin.site.register` का उपयोग करके रजिस्टर करें।

 
# main/admin.py
from django.contrib import admin
from .models import Items

admin.site.register(Items)



When to Use? (कब उपयोग करें?)
For simple models without any customization. - छोटे मॉडल्स के लिए जिनमें कोई कस्टमाइज़ेशन न हो। 

 2. Decorator-Based Registration with ModelAdmin (डेकोरेटर-आधारित पंजीकरण ModelAdmin के साथ)

This method uses the `@admin.register` decorator to register a model with a `ModelAdmin` class for customization. यह विधि `@admin.register` डेकोरेटर का उपयोग करके मॉडल को `ModelAdmin` क्लास के साथ कस्टमाइज़ेशन के लिए रजिस्टर करती है।
from django.contrib import admin
from .models import User

@admin.register(User)
class UserAdmin(admin.ModelAdmin):
    list_display = ('id', 'name', 'email', 'password')


When to Use? (कब उपयोग करें?)
When you need to customize the admin interface (e.g., columns, filters, search). - जब एडमिन इंटरफ़ेस में कस्टमाइज़ेशन की आवश्यकता हो (जैसे कॉलम्स, फ़िल्टर्स, सर्च)।

3. Manual Registration with ModelAdmin (मैनुअल पंजीकरण ModelAdmin के साथ)

Similar to the decorator approach but uses explicit syntax. डेकोरेटर विधि के समान है लेकिन स्पष्ट सिंटैक्स का उपयोग करता है। 
from django.contrib import admin
from .models import User

class UserAdmin(admin.ModelAdmin):
    list_display = ('id', 'name', 'email', 'password')

admin.site.register(User, UserAdmin)


When to Use? (कब उपयोग करें?)
When you prefer separating registration and customization for clarity. - जब आप स्पष्टता के लिए रजिस्ट्रेशन और कस्टमाइज़ेशन को अलग रखना चाहते हों। --- Advanced Techniques (उन्नत तकनीकें) Proxy Models (प्रॉक्सी मॉडल्स) Allows you to define different admin configurations for the same model. यह आपको एक ही मॉडल के लिए विभिन्न एडमिन कॉन्फ़िगरेशन परिभाषित करने की अनुमति देता है।
from django.contrib import admin
from .models import User

class UserAdmin(admin.ModelAdmin):
    list_display = ('id', 'name', 'email', 'password')

@admin.register(User)
class ProxyUserAdmin(UserAdmin):
    list_display = ('id', 'name')


Unregister and Re-register (अनरजिस्टर और री-रजिस्टर)

Modify the admin behavior for third-party models by unregistering and re-registering them. थर्ड-पार्टी मॉडल्स के एडमिन व्यवहार को संशोधित करने के लिए उन्हें अनरजिस्टर और री-रजिस्टर करें।
from django.contrib import admin
from someapp.models import ThirdPartyModel

admin.site.unregister(ThirdPartyModel)

@admin.register(ThirdPartyModel)
class CustomThirdPartyAdmin(admin.ModelAdmin):
    list_display = ('id', 'custom_field')


Conclusion (निष्कर्ष) 

  • Use admin.site.register for simple models.सरल मॉडल्स के लिए admin.site.register का उपयोग करें।
  • Use @admin.register for customization. कस्टमाइज़ेशन के लिए @admin.register का उपयोग करें।
  • For advanced needs, explore proxy models or unregister/re-register techniques. उन्नत आवश्यकताओं के लिए, प्रॉक्सी मॉडल्स या अनरजिस्टर/री-रजिस्टर तकनीकों का उपयोग करें।





See full Django Docs by clicking here, Django Docs देखने के लिए यहां स्पर्श करें

टिप्पणियाँ

इस ब्लॉग से लोकप्रिय पोस्ट

Meta Class in Django Form , Meta Class का Django-Model Forms में उपयोग

GitHub उपयोगकर्ता मार्गदर्शिका और दस्तावेज़