Hafiz
Hafiz

Reputation: 4267

dependent fields in django admin

I want to add, some fields depending on others. I have city and country model. I can include country as foreign key in city model. And then if I will add both city and country in another model ( say content) then will it be just like dependent selectboxes? like cities will be shown based on selected country via ajax? If not then what is correct way? and also is there a way to add city on the spot while adding main content data if city is not already on list?

So are above possible by using django admin or is it that django don't give? If not then how can it be done in django autogenerated admin?

Upvotes: 2

Views: 3689

Answers (2)

danodonovan
danodonovan

Reputation: 20343

You can do exactly what you ask using django-smart-selects

Hope that helps...

Upvotes: 3

Ska
Ska

Reputation: 6888

I can include country as foreign key in city model

This seems like a good idea.

And then if I will add both city and country in another model ( say content) then will it be just like dependent selectboxes? like cities will be shown based on selected country via ajax?

No, it will not get filtered automatically, you will need to write that code yourself. Both in admin and on frontend.

and also is there a way to add city on the spot while adding main content data if city is not already on list?

You will get this in the admin area.

Go ahead and start doing it, and when you run into specific problems, post them here if you can't solve it. Also read the Django docs, it is pretty elaborate on the topic of models.

Upvotes: 1

Related Questions