What am I doing wrong?
In django admin-panel I want to show/hide field(s) based on the choice dropdown. Also the, choice dropdown lies on parent foreign-key related model and the fields that are to be shown/hidden lies in child model or as an stacked inline.
I've followed this solution(stack overflow), but no success.
models.py
from django.db import models
CHOICES = (
('video', 'Video'),
('text', 'Text'),
('question', 'Question'),
)
class Section(models.Model):
content_type = models.CharField(max_length=32)
@property
def contents(self):
return self.content_set.all()
class Content(models.Model):
content = models.ForeignKey(Section, on_delete=models.DCASCADE)
video = models.FileField()
text = models.TextField()
question = models.CharField(max_length=512)
admin.py
from django.contrib import admin
from .models import Section, Content
from .forms import DropdownModelForm
class ContentInline(admin.StackedInline):
model = Content
fieldsets = (
(None, {
'fields': (('video',),),
'classes': ('vid',)
}),
(None, {
'fields': (('text',),),
'classes': ('txt',)
}),
(None, {
'fields': (('question',),),
'classes': ('ques',)
})
)
class Media:
js = ('one/js/base.js',)
@admin.register(Section)
class SectionAdmin(admin.ModelAdmin):
form = DropdownModelForm
inlines = (ContentInline,)
forms.py
from django import forms
from .models import Section, CHOICES
class DropdownModelForm(forms.ModelForm):
class Meta:
model = Section
fields = ('content_type',)
widgets = {
'content_type': forms.Select(choices=CHOICES)
}
base.js
(function($) {
$(function() {
var selectField = $('#id_content_type'),
verified_1 = $('.vid'),
verified_2 = $('.txt'),
verified_3 = $('.ques');
function toggleVerified(value) {
if (value === 'video') {
verified_1.show();
verified_2.hide();
verified_3.hide();
} else if (value === 'text') {
verified_1.hide();
verified_2.show();
verified_3.hide();
} else if (value === 'question') {
verified_1.hide();
verified_2.hide();
verified_3.show();
}
}
// show/hide on load based on pervious value of selectField
toggleVerified(selectField.val());
// show/hide on change
selectField.change(function() {
toggleVerified($(this).val());
});
});
})(django.jQuery);
settings.py
from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent.parent
.
.
.
INSTALLED_APPS = [
'one.apps.OneConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
.
.
.
STATIC_URL = '/static/'
STATICFILES_DIRS = [
BASE_DIR / "static",
]
Again, What am I doing wrong?
In django admin-panel I want to show/hide field(s) based on the choice dropdown. Also the, choice dropdown lies on parent foreign-key related model and the fields that are to be shown/hidden lies in child model or as an stacked inline.
I've followed this solution(stack overflow), but no success.
Thank you very much for expending your valuable time & giving a look on my poor code