Group ManyToMany in Django admin -
i have model "company" having many many relationships country , city while country , city have 1 many relationship between them. problem when loading "enterprise" have select countries atienede company , cities serve, list of cities long , cities mixed without distinguishing country are. i'd group cities country.
it django-smart-selects, plugin not work in many many relationships. me adapt plugin work many many relationship or comment me if think of alternative.
thank much!
class company(models.model): name = models.charfield(max_length=255) countries = models.manytomanyfield(country) cities = models.manytomanyfield(city) class country(models.model): name = models.charfield(max_length=255) class city(models.model): name = models.charfield(max_length=255) country = models.foreignkey(country)
here solution how can solve in admin smart_selects:
define through model manytomany relationship (company city). in through model define relationship city groupedforeignkey. make inline in admin conpany through model.
class country(models.model): name=models.charfield(max_length=50) class city(models.model): name=models.charfield(max_length=50) country=models.foreignkey(country) class ccrel(models.model): city= groupedforeignkey(city, "country") company = models.foreignkey("company") class company(models.model): name=models.charfield(max_length=50) country = models.manytomanyfield(country,through="ccrel") and admin.py:
class cinline(admin.tabularinline): model = ccrel class cadmin(admin.modeladmin): inlines=[cinline] admin.site.register(company,cadmin) it should work chainedforeignkey in similar manner.
you should remove country field company model redundant.
Comments
Post a Comment