Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding instances from django admin

This is how my classes look.

class School(models.Model):
    state = models.ForeignKey(State, on_delete=models.CASCADE)
    region = models.ForeignKey(Region, on_delete=models.CASCADE)
    cluster = models.ForeignKey(Cluster, on_delete=models.CASCADE)
    school_name = models.CharField(max_length=250)
    facilitator = models.CharField(max_length=250)
    f_number = models.IntegerField()
    f_email = models.EmailField()
    school_logo = models.FileField(default='')
    school_strength = models.IntegerField()

In which state, region, cluster are also classes. I have tried to create a network of schools with classification in 3 levels i.e state, region, cluster. I have assumed that when I add schools from django admin I thought it might filter regions if I select state and filter clusters when I select region so that it would be easy. But It doesn't seem to work. Though after selecting a particular state and region while adding a school in the cluster dropdown all the clusters from all the states and regions are coming.

Is there a solution for this or as of now we don't have such option in django?

like image 942
venkat mani sai Avatar asked Feb 16 '26 07:02

venkat mani sai


1 Answers

Try ChainedForeignKey from django-smart-selects package it works like a magic.

Installation of smart_selects

pip install django-smart-selects

project settings.py

INSTALLED_APPS=[
    . . . . .
    . . . . .
    'smart_selects',
    . . . . .
]

project urls.py

urlpatterns = [
    . . . . .
    url(r'^admin/', include(admin.site.urls)),
    url(r'^chaining/', include('smart_selects.urls')),
    . . . . .
]

See the installation of django-smart-selects

Working of ChainedForeignKey

from smart_selects.db_fields import *

class State(models.Model):
    stateName = models.CharField(max_length=500)
    . . . . 

class Region(models.Model):
    state = models.ForeignKey(State, on_delete=models.CASCADE)
    regionName = models.CharField(max_length=500)
    . . . . .
    . . . . .


class Cluster(models.Model):
    state = models.ForeignKey(State, on_delete=models.CASCADE)
    region = ChainedForeignKey(Region,chained_field="state",chained_model_field="state", show_all=False, auto_choose=True, sort=False, on_delete=models.CASCADE)
    clusterName = models.CharField(max_length=500)
    . . . . .
    . . . . .  



class school(models.Model):
    state = models.ForeignKey(State, on_delete=models.CASCADE)
    region = ChainedForeignKey(Region, chained_field="state",chained_model_field="state", show_all=False, auto_choose=True, sort=False, on_delete=models.CASCADE)
    cluster = ChainedForeignKey(Cluster, chained_field="region",chained_model_field="region", show_all=False, auto_choose=True, sort=False, on_delete=models.CASCADE)
    schoolName = models.CharField(max_length=500)
    . . . . 
    . . . .

Try this, it works so well in admin area.

like image 163
Astik Anand Avatar answered Feb 18 '26 19:02

Astik Anand



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!