Accesing a property and methd in the admin site

2007-03-12 Thread hass

I just got my first django app up: http://brackets.bracketboy.net/ Not
very impressive, but I'm still excited about it. anyway . . .


I'm having trouble accesing a method through the admin interface as
part of a choices list. It seems to work fine throughout the site and
the admin interface, but not in choices selection. In the choices I
get .  Any ideas?

Some of the relevant code:

  def _get_team_2(self):
  if self.round == 1 :
return
Team.objects.filter(seed=self.seed_2).filter(region=self.region)
[0].name

  else:
prev_game =   Game.objects.get( id = self.id ).prev_game_2_id
prev_winner = Game.objects.get( id = prev_game ).winner.id
returnTeam.objects.get( id = prev_winner ).name

  team_2 = property( _get_team_2)

  winner = models.IntegerField(choices=((team_1.id, team_1),
(team_2.id, team_2),),)


(Since the winner field is just storing id's, I had initlaly hoped to
do it with a foreign key and limit_choices_to, but I wasn't able to
even get that close. But that's a question for another day, unless
somebody has the answer)


--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Accesing a property and methd in the admin site

2007-03-12 Thread Malcolm Tredinnick

On Mon, 2007-03-12 at 17:37 +, hass wrote:
> I just got my first django app up: http://brackets.bracketboy.net/ Not
> very impressive, but I'm still excited about it. anyway . . .
> 
> 
> I'm having trouble accesing a method through the admin interface as
> part of a choices list. It seems to work fine throughout the site and
> the admin interface, but not in choices selection. In the choices I
> get .  Any ideas?
> 
> Some of the relevant code:
> 
>   def _get_team_2(self):
>   if self.round == 1 :
> return
> Team.objects.filter(seed=self.seed_2).filter(region=self.region)
> [0].name
> 
>   else:
> prev_game =   Game.objects.get( id = self.id ).prev_game_2_id
> prev_winner = Game.objects.get( id = prev_game ).winner.id
> returnTeam.objects.get( id = prev_winner ).name
> 
>   team_2 = property( _get_team_2)
> 
>   winner = models.IntegerField(choices=((team_1.id, team_1),
> (team_2.id, team_2),),)

It's not completely clear what is going wrong here, but this
construction (of the "winner" attribute) looks dangerous. The "choices"
attribute is largely meant to take a sequence of static pairs, since it
is populated at import time. You seem to be trying to populate it with
something dynamic, which could fail in a number of interesting ways.

It is possible to use an iterator as the argument for the "choices"
attribute and maybe that is what you want to use (see the wiki for
details), but the way you are doing it at the moment looks like it's
bound to not work consistently.

Regards,
Malcolm



--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---