Home > Mobile >  Is there a way to turn on/off a particular field from Django admin?
Is there a way to turn on/off a particular field from Django admin?

Time:11-17

I want to implement a setting in my Django admin dashboard where I can disable/enable a particular field. If I should disable the field, the data in that field will not be rendered on the web page. If I should enable the field, then the data will be shown in the webpage. I only want to do this from my the admin dashboard.

This is my models.py:

class Product(models.Model):
    category = models.ForeignKey(Category, related_name='products', on_delete=models.CASCADE)
    name = models.CharField(max_length=100, unique=True)
    slug = models.SlugField(max_length=100, unique=True)
    image = models.ImageField(upload_to='products/')
    description = models.TextField()
    quantity = models.CharField(max_length=10)
    price = models.DecimalField(max_digits=10, decimal_places=2)
    available = models.BooleanField(default=True)

This is the admin.py:

@admin.register(Product)
class ProductAdmin(admin.ModelAdmin):
    list_display = ('name', 'category', 'slug', 'price', 'available')
    list_filter = ('category', 'available')
    list_editable = ('price', 'available')
    prepopulated_fields = {'slug': ('name',)}

I want to have the ability to enable/disable (maybe like a toggle button) the description field from my admin dashboard. Is there a way to implement this in my admin.py file? How can I go about this? Thanks.

Edit: For better understanding (as suggested the comment), I want to implement a master toggle in the admin dashboard which turns on/off all descriptions for every instance shown to the user.

CodePudding user response:

The Django constance (no association to myself) might do what you want. It doesn't necessarily store the toggle you're describing in any way connected to your model, but it will allow you to have editable settings. This can be used in your form definition or passed to your context through either the view or a context processor.

CodePudding user response:

Several ways to do so.

1 permission to admin user. 2 bool show or not to Object. 3 New Admin view.

1

You may add a permission to see this field and create a method in admin.py to see it or return None depending on permission value.

In admin.py in

class YourAdminClass(admin.ModelAdmin)
    def get_queryset(self, request):
         queryset= super(YourAdminClass, self).get_queryset(request)
         self.request = request
         return queryset 

    def mobile_phone(self, obj):
        return obj.user.mobile_phone if self.request.user.has_special_permission else None

2

You may add a bool to the object instead of permission to user (show or do not show in admin) and change the query set as in option 1 (this way you may control it from the client side FE)

def get_queryset(self, request):
    qs = super(YourAdminClass, self).get_queryset(request)
    return qs.filter(SHOW_MY_FIELD=True)

3

Create anew Admin view NoFieldAdmin and alternative to Admin and show or not show the link in main admin via the admin url in main admin.py file

class NoFieldAdminSite(AdminSite):
    pass
    #or overwrite some methods for different functionality

NoFieldAdmin= NoFieldAdminSite(name="nofield_admin")   

in urls.py
        urlpatterns = [
            url(r'^admin/', admin.site.urls),
            url(r'^NOFIELD-admin/', NoFieldAdmin.urls),
  • Related