On Fri, Mar 13, 2009 at 2:53 PM, joemanfoo <joeman...@gmail.com> wrote:

>
> Hi there,
>
> I'm very new to both Python and Django, so please pardon my ignorance
>
>
> Here's what I'd like to do...
> I've two models, one foriegnKey'ed to the other.
>
> in models.py:
> class Market(models.model):
>    name = models.CharField(max_length=55)
>
> class Alias(models.model):
>    market_id = models.ForeignKey(Market)
>    alias = models.CharField(max_length=255)
>    uripatern = models.CharField(max_length=255)
>
>
> in admin.py:
> class AliasAdmin(admin.ModelAdmin):
>    list_display = ('market_name', 'alias', 'uripatern')
>    list_filter  = ['market_id']
>    search_fields = ['alias', 'uripatern', 'market_id__name']
>
>
> So as you can see, I'd like to include the Market.name field rather
> than the market_id.  I'd also like to have the search on aliases also
> look on the Market.name
>
> I've tried to add a function in the class Alias:
> def market_name(self):
>        return self.market_id__name
> But this just shows "None" in the admin screen for Aliases.
> Thank you for your time and any help.


Admin displaying "(None)" for something that was supposed to be returned by
a callable in list_display is a clue that the callable didn't return a valid
value (raised an exception).  You can see why if you try calling your
market_name function from a "python manage.py shell" session:

>>> from ttt.models import Alias
>>> a = Alias.objects.all()[0]
>>> a.market_name()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "D:\u\kmt\software\web\playground\ttt\models.py", line 66, in
market_name
    return self.market_id__name
AttributeError: 'Alias' object has no attribute 'market_id__name'
>>>

The double underscore is used for specifying lookups (i.e. filter, get,
search_fields, ...) that span relationships across foreign keys.  When you
are dealing directly with model object instances, though, you just use the
regular Python dot notation to access the related object instance:

   def market_name(self):
      return self.market_id.name

If you make that change and try again from a manage.py shell:

>>> from ttt.models import Alias
>>> a = Alias.objects.all()[0]
>>> a.market_name()
u'Xyzzy'
>>>

Now it works.  A couple of notes though:

First, you seem to be going about this the hard way.  The more natural way
to get the name to display in the Admin is to define a __unicode__ method on
Market that returns the name:

   def __unicode__(self):
      return self.name

Then, just including 'market_id' in your list_display for AliasAdmin will
result in the name being displayed, since Admin (for ForeignKey fields)
displays whatever the relatedd model's unicode method returns.

Which leads to my second note: you might want to rethink the name
'market_id' for the ForeignKey field in Alias.  When you work with model
instance objects in Django the ForeignKey field in Alias can be used to
access the whole related object, not just the related object's id field, so
it's potentially a little confusing to have the _id on the end of your field
name.

Karen

--~--~---------~--~----~------------~-------~--~----~
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