Re: How to change the option text in a form's select-tag?

2009-08-09 Thread Léon Dignòn

I solved it with __unicode__ in the model class.

On Aug 7, 1:38 pm, Léon Dignòn  wrote:
> Hello,
>
> I installed django-profiles and added an extended UserProfile. One
> template displays all fields which are necessary to edit a users
> profile. Every user can choose a favorite aeroplane. Every aeroplane
> is built by one manufacturer.
>
> I print the edit form in the template with {{ form }}. The form
> dispays a select tag to chose a favorite aeroplane. Everything works
> fine.
>
> I want to make a little change on how the selectfielddisplays the
> options. I would like toaddthe manufacturer to the option string. Is
> there an easy way to insert the manufacturer to the option? I want to
> avoid as much code as possible since the django-profiles app provides
> views and forms. I hope not to write any own. Maybe I can change the
> model so that it alwas displays Manufacturer+Aeroplane when called?
>
> I just tried to insert the following code in the Aeroplane class:
> def __unicode__(self):
>         return self.name + manufacturer.name
>
> 
> Here some detailed explanation and code:
>
> My selectfieldlooks like this:
> 
>   Plane1
>   Plane2
>   Plane3
> 
>
> My selectfield*should* looks like this:
> 
>   Manufacturer1 Plane1
>   Manufacturer2 Plane2
>   Manufacturer3 Plane3
> 
>
> Here is the models.py
>
> class Aeroplane(models.Model):
>     name = models.CharField(max_length=32, unique=True)
>     year_of_construction = models.PositiveSmallIntegerField()
>     manufacturer = models.ForeignKey(Manufacturer)
>
>     def __unicode__(self):
>         return self.name
>
> class UserProfile(models.Model):
>     user = models.ForeignKey(User, unique=True)
>     favorite_aeroplane = models.ForeignKey(Aeroplane, blank=True)
>
>     def get_absolute_url(self):
>         return ('profiles_profile_detail', (), { 'username':
> self.user.username })
>     get_absolute_url = models.permalink(get_absolute_url)
>
> Here is the template:
>
> {% extends "base.html" %}
> {% block content %}
>     
>       {{ form.as_p }}
>       
>     
> {% endblock content %}
--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



How to change the option text in a form's select-tag?

2009-08-07 Thread Léon Dignòn

Hello,

I installed django-profiles and added an extended UserProfile. One
template displays all fields which are necessary to edit a users
profile. Every user can choose a favorite aeroplane. Every aeroplane
is built by one manufacturer.

I print the edit form in the template with {{ form }}. The form
dispays a select tag to chose a favorite aeroplane. Everything works
fine.

I want to make a little change on how the select field displays the
options. I would like to add the manufacturer to the option string. Is
there an easy way to insert the manufacturer to the option? I want to
avoid as much code as possible since the django-profiles app provides
views and forms. I hope not to write any own. Maybe I can change the
model so that it alwas displays Manufacturer+Aeroplane when called?

I just tried to insert the following code in the Aeroplane class:
def __unicode__(self):
return self.name + manufacturer.name



Here some detailed explanation and code:



My select field looks like this:

  Plane1
  Plane2
  Plane3


My select field *should* looks like this:

  Manufacturer1 Plane1
  Manufacturer2 Plane2
  Manufacturer3 Plane3




Here is the models.py

class Aeroplane(models.Model):
name = models.CharField(max_length=32, unique=True)
year_of_construction = models.PositiveSmallIntegerField()
manufacturer = models.ForeignKey(Manufacturer)

def __unicode__(self):
return self.name

class UserProfile(models.Model):
user = models.ForeignKey(User, unique=True)
favorite_aeroplane = models.ForeignKey(Aeroplane, blank=True)

def get_absolute_url(self):
return ('profiles_profile_detail', (), { 'username':
self.user.username })
get_absolute_url = models.permalink(get_absolute_url)



Here is the template:

{% extends "base.html" %}
{% block content %}

  {{ form.as_p }}
  

{% endblock content %}
--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---