1

I have tried this a lot and searched about it too but not found any solution, I want that in admin panel when admin selects the category choice field and the subcategory choice field changes according to it, like I have a category called 'Shoes' and 'watches' and sub-category accordingly for shoes - 'puma', 'Adidas' and watches-'titan', 'G-shock'. when admin selects category 'shoes' subcategory choices should be 'puma' and 'Adidas' and if he/she selects category 'watches' subcategory choices should be 'titan' and 'G-shock'.

here is my models.py

from django.db import models
from django.db import connection

# Create your models here.

class categorie(models.Model):
    name = models.CharField(max_length=100, unique=True)

    def __str__(self):
        return self.name

class sub_categorie(models.Model):
    sub_name = models.CharField(max_length=100)
    catagory_name = models.ForeignKey(categorie, on_delete=models.CASCADE, default=1)

    def __str__(self):
        return self.sub_name

class Products(models.Model):
    Product_Name = models.CharField(max_length=100)
    price = models.FloatField()
    Description = models.TextField()
    ctg = models.ForeignKey(categorie, on_delete=models.CASCADE,blank = False,  default=1)
    sub_category = models.ForeignKey(sub_categorie, on_delete=models.CASCADE,blank = False)

1 Answers1

0

Doing this is possible when you have UI control. I would suggest first having your custom JS in the Admin Panel to Add Field and its choices based on the value of category field.

To add choices to the Admin Panel you can add choices attribute in your model field.

product_type = [('shoes','Shoes'),('watches','Watches')]

class categorie(models.Model):
    name = models.CharField(max_length=100, unique=True,choices=product_type)

Now you need to add js to Admin Panel :How to load Custom JS in Django Admin Home You can try this or a similar approach and check for the value of the field categorie in your JS and when it changes to lets say 'Shoes' then you need to add the field sub category with choices in select as 'Adidas,Puma,Nike,Reebok etc'.


UPDATE 1: How to add custom JS in particular field: When you have model in your app ,lets take the same categorie you have name as your field.You go to the ADMIN PANEL for this model, your field will have id as id_name as default.So your input will look something like.

<input type="text" name="name" class="vTextField" maxlength="100" id="id_name">

So you now have the id of your field in your admin forms.So for every fieldname you have id as id_fieldname. You can add an Event Listener in JS for that field on value change.

document.getElementById('id_fieldname').addEventListener('change',function(){
#Do what you want here
});

So in that function either you can call another function that adds the select input field which have the required options or have the field already there and just insert options in the select field.

Dushyant Deshwal
  • 388
  • 4
  • 17
  • Hey Dushyant, thanks for your fast answer but how to add custom js for perticuler field like in class products i wanted to do dynamic choices in subcategory field how to add in field – Devarsh Suthar Jul 18 '20 at 08:41
  • @DevarshSuthar I have updated the answer on how to add JS for particular field.To add and insert html you need to look at `insertAdjacentHTML()` and `innerHTML` function and property of an element for JavaScript. Mozilla Developer Network (MDN) and W3 Schools are good sources to learn from. – Dushyant Deshwal Jul 18 '20 at 20:04
  • Thanks I understood really well but I have another doubt that there are lots of django admin files and blocks like {% extends 'admin/base.html'%} and in that we have to call block {% block extrahead%} and in that block we add this file. So which html file we have to extend for this task and which block need to call? – Devarsh Suthar Jul 19 '20 at 12:17
  • @DevarshSuthar You can just add the JS in the `admin/base.html`, you can check in the base_site.html file that is extends the base.html and then base_site is extended by the change_form.html so add the JS in base.html. You can also add a block in admin/base.html in the header scripts section and use that to add script in change_form.html itself. I hope this helps,please mark this as answered. – Dushyant Deshwal Jul 20 '20 at 03:32