PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, October 19, 2022

[FIXED] How can I make one of the fields in admin.TabularInline conditional?

 October 19, 2022     admin, django, if-statement, inline, python     No comments   

Issue

Is there a way how can I make one of the fields in admin.TabularInline conditional?

for example

class ParameterInline(admin.TabularInline):
    form = ParameterForm
    fields = ["ParameterA", "ParameterB"]

What if I wanted to display the ParameterB only if something else was set to, for example, True?

Thanks in advance.


Solution

You can achieve this by overriding the get_fields method:

class ParameterInline(admin.TabularInline):
    form = ParameterForm
    fields = ["ParameterA"]
    
    def get_fields(self, request, obj=None):
        fields = super(ParameterInline, self).get_fields(request=request, obj=obj)
        if True:
            return fields + ['ParameterB']
        return fields 


Answered By - Marcell Erasmus
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing