Re: How do I get a field value instead of in an admin selectbox?

Wed, 17 Jun 2009 07:08:05 -0700

The answer was given to you in the previous post.

Whatever __unicode__() returns from your model is what will be
displayed.

So, on your model, define a __unicode__ method:

e.g.

  class Person(...):
    def __unicode__(self):
       return self.name

On Jun 18, 12:03 am, DaveB <d...@davebritton.com> wrote:
> > Your question is very difficult to read - needs paragraphs.
>
> > But I think the answer to your problem is to define a __unicode__
> > method on the target model.
> > --
> > DR.
>
> I will try to be clearer and briefer:
>
> Django's built-in admin system automatically generates data-entry
> forms for the fields in a table. If those fields are ForeignKey types
> such as OneToOneField, the data entry system generates a <select> box
> to allow the user to pick an entry from the ForeignKey table. I am
> getting these <select> <option> choices  all displayed as the same
> literal "<ForeignKey tablename> object".
>
> I would like them to be displayed instead as
> "<ForeignKey_table.object.field>.value" For instance, if the table I
> am doing data entry into has a field which is a OneToOneField pointer
> to a table named People which has a field called name, I want the
> generated <select> <option> to show as the values of each People
> record's People.name field. It currently (as a default behavior) seems
> to only display the same literal  "People object" for every record in
> the drop-down selection box .
>
> Here is a minimal example of what I am doing:
> # =====   from admin.py  ===========
> class PersonInline(admin.TabularInline):
>         model = Person
> class PersonAdmin(admin.ModelAdmin):
>         list_display=('name',)
>
> class MomAdmin(admin.ModelAdmin):
>         list_display=('myname','PTArole')
>         def myname(self,obj):
>                 pname=Person.objects.get(personID=obj.personID_id).name
>                 if pname is None: myname="No Person found for %s"%str
> (obj.personID_id)
>                 return pname
>
> # ====== from models.py ==============
> class Person(models.Model):
>         personID=models.AutoField (primary_key=True)
>         name=models.CharField(max_length=20)
>
> class Mom(models.Model):
>         momID=models.AutoField (primary_key=True)
>         personID=models.OneToOneField('Person',to_field='personID')
>         PTA_roles=(('O','officer'),('C','on committee'),('A','active'),
> ('I','inactive'))
>         PTArole=models.CharField(max_length=1,choices=PTA_roles)
>
> =====================================
>
> When the admin interface goes to add/change Mom, it shows the
> PTA_roles correctly, but the PersonID field is shown as "Person
> object"
>
> What should I do to have the personId field selection display
> Person.name instead?
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to