I have two models for store and city:
class City(models.Model):
name = models.CharField()
slug = models.SlugField()
class Store(models.Model):
name = models.CharField()
slug = models.SlugField()
city = models.ForeignKey(City)
If I have my Add Store url designed as
r^’mysite.com/city/(?[-\w]+)/venue/add$’
where the represents the City.slug field can I initialize a StoreForm that automatically populates the Store.city field from the url data?
In your template, define your link as follows.
Or :
In your url conf, define your url like:
So, you can pass the value of relelvant_slug_text variable to your url as slug_text, and in your function definiton:
So , you can pass text value to your funcrtion with slug_text parameter…
EDIT:
There are tow ways…
One:
Crete your city selection form using ModelForm…, then inthe second step, use posted data to populate your form again like:
then you can render this form to your template…
But if it is not possible o use this, yo ucan do the following…
Since you are using ModelForm to create your forms:
im your function, just override city field, but at this point, since you use modelform, your form thml will be created as
So, you have record id’s as option values. And you have slug_field values to initialize your field. So you have to get related city to use it…
Now you can override the form, but you must do it before you pass your form to a variable to render to your template…