简体   繁体   中英

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 ie 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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM