Reputation: 225
I'm having a problem getting the django "smart-selects" app to function properly. It will not populate the select for my chained field. The "readme" file on github "https://github.com/digi604/django-smart-selects" gives the following instructions.
I find the instructions a little cryptic, and they do not show the model for the Continent and County tables, to make it easier.
I have a previous question posted regarding this which I did not get a response to. It has more info about my specific model. "http://stackoverflow.com/questions/9155350/django-smart-selects-second-level-wont-populate"
Hopefully there are some "django-smart-selects" experts that can get me over the hump. Thanks
from smart_selects.db_fields import ChainedForeignKey
class Location(models.Model)
continent = models.ForeignKey(Continent)
country = ChainedForeignKey(
Country,
chained_field="continent",
chained_model_field="continent",
show_all=False,
auto_choose=True
)
area = ChainedForeignKey(Area, chained_field="country", chained_model_field="country")
city = models.CharField(max_length=50)
street = models.CharField(max_length=100)
Upvotes: 3
Views: 3615
Reputation: 21
You should insert this in the urls.py:
url(r'^chaining/', include('smart_selects.urls')),
This is what I did and worked:
#=================================
#models.py
from smart_selects.db_fields import ChainedForeignKey
class Continent(models.Model):
continent = models.CharField(max_length = 45)
def __unicode__(self):
return unicode(self.continent)
class Country(models.Model):
continent = models.ForeignKey(Continent)
country = models.CharField(max_length = 45)
def __unicode__(self):
return unicode(self.country)
class Area(models.Model):
country = models.ForeignKey(Country)
area = models.CharField(max_length = 45)
def __unicode__(self):
return unicode(self.area)
class Location(models.Model):
continent = models.ForeignKey(Continent)
country = ChainedForeignKey
(
Country,
chained_field = "continent",
chained_model_field = "continent",
show_all = False, auto_choose = True
)
area = ChainedForeignKey(Area, chained_field = "country", chained_model_field = "country")
city = models.CharField(max_length = 50)
street = models.CharField(max_length = 100)
#=================================
#admin.py
from yourapp.models import Continent
from yourapp.models import Country
from yourapp.models import Area
from yourapp.models import Location
class ContinentAdmin(admin.ModelAdmin):
pass
class CountryAdmin(admin.ModelAdmin):
pass
class AreaAdmin(admin.ModelAdmin):
pass
class LocationAdmin(admin.ModelAdmin):
pass
admin.site.register(Continent, ContinentAdmin)
admin.site.register(Country, CountryAdmin)
admin.site.register(Area, AreaAdmin)
admin.site.register(Location, LocationAdmin)
Upvotes: 2
Reputation: 225
That is the way I interpreted it dragonx. But I am obviously missing something.
I am thinking the other classes look like this
class Continent(models.Model):
continent = models.CharField(max_length=45)
def __unicode__(self):
return self.continent
class Country(models.Model):
continent = models.ForeignKey(Continent)
country = models.CharField(max_length=45)
def __unicode__(self):
return self.county
with "continent" in the Country class being an integer field that corresponds to the ID field of the Continent class (ForeignKey/field) upon which the join is made. Is this the way you see it?
Upvotes: 0
Reputation: 15143
In the given example of for country = ChainedForeignKey() field, the chained_field="continent" refers to the "continent" field in Location class. The chained_model_field="continent" refers to a "continent" field inside the Country model (which is not shown in the example).
Upvotes: 0