accessing foreign key value in template select related
Hi i have a UserProfile that has a city_id as a foreign key to cities
table. when i use {{ profile_edit_form.city_id}} in the template i get
the id of the city that is stored in my userprofile showing as the
value instead of the city name. Would the select related fix this
issue and lookup the city name form the city table based on the city
id in the userprofile? ive tried useing select related on my queries
but it gives me an error saying userprofile has no select related. any
help would be much appreciated.
code
models.py
#this class was genrated using inspectdb
class Cities(models.Model):
id = models.IntegerField(primary_key=True, db_column='ID') # Field
name made lowercase.
country = models.CharField(unique=True, max_length=6)
region = models.CharField(unique=True, max_length=9)
url = models.CharField(max_length=150)
name = models.CharField(unique=True, max_length=150)
latitude = models.FloatField()
longitude = models.FloatField()
class Meta:
db_table = u'cities
class UserProfile(models.Model):
user = models.OneToOneField(User)
city_id =
models.ForeignKey(Cities,blank=True,db_column='city_id')#tried adding
this too related_name='cities'
forms.py
class ProfileEditForm(ModelForm):
class Meta:
model = UserProfile
views.py
def edit(request):
c = {}
c.update(csrf(request))
profile_edit_form =
ProfileEditForm(instance=request.user.get_profile()) #tried
adding .select_related() here
if request.method == "POST":
profile_edit_form =
ProfileEditForm(request.POST,request.FILES,instance=request.user.get_profile())
if profile_edit_form.is_valid():
p = profile_edit_form.save()
city = Cities.objects.get(pk=request.POST['cid'])
p.city_id = city
p.save()
c['profile_edit_form'] = profile_edit_form
return render_to_response('profile/edit.html', c,
context_instance=RequestContext(request))
template
in my template {{ profile_edit_form }} #shows the city id not the
actual city name refered to by the id ?
thanks
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home