Ok, I found a way, but it's still not perfect.
class GoalForm(forms.ModelForm):
class Meta:
model = Goal
def __init__(self, *args, **kwargs):
super(GoalForm, self).__init__(*args, **kwargs)
self.fields['goal_scorer'].queryset =
Player.objects.filter(gameroster__game=self.instance.game)
class GoalInline(admin.TabularInline):
model = Goal
extra = 4
#form = GoalForm
class GameAdmin(admin.ModelAdmin):
list_display = ('date_time', 'home_team', 'opponent_team',
'is_home_game', 'season', 'league', 'type', 'result')
list_filter = ['league', 'season']
inlines = [GameRosterInline, GoalInline, PenaltyInline]
ordering = ('date_time',)
That works as long as I edit the model game not inline. The selection
of player gets limited as I want.
When I try to edit it inline in the game, the GoalForm seems not to
work. It's going to be ignored.
When I try to comment in "form = GoalForm", the whole app doesn't run
anymore. I got a DoesNotExist exception, but I think this is because
it can't register the form..
Any ideas who to activate my custom form for the inline mode?
Chris
2009/10/12 Christian Wittwer <[email protected]>:
> Hi,
> I have a question regarding the admin interface of django.
>
> My models
>
> class Team(models.Model):
> name = models.CharField(max_length=10)
>
> class Player(models.Model):
> lastname = models.CharField(max_length=60)
>
> class Goal(models.Model):
> game = models.ForeignKey(Game)
> goal_scorer =
> models.ForeignKey(Player,related_name='goal_scorer',blank=True,null=True)
>
> class Game(models.Model):
> home_team = models.ForeignKey(Team,related_name='home_team')
>
> class GameRoster(models.Model):
> player = models.ForeignKey(Player)
> game = models.ForeignKey(Game)
>
> Each Goal is scored by a Player in a Game. I'm using a inline form to
> insert goals for a game.
> The players are somehow connected to the team (gameroster), and
> therefore to the game.
>
> I found the place to hook in to limit the choices of player.
>
> class GoalInline(admin.TabularInline):
> model = Goal
> extra = 4
>
> def formfield_for_foreignkey(self, db_field, request, **kwargs):
> if db_field.name == "goal_scorer":
> kwargs["queryset"] =
> Player.objects.filter(gameroster__game=self.game)
> return db_field.formfield(**kwargs)
>
> return super(GoalInline,
> self).formfield_for_foreignkey(db_field, request, **kwargs)
>
>
> The missing point is now: How to access game, which is connected to
> the goal to limit the players?
> I tried self.game as you can see, but that doesn't work.
> How can I access the model from within this function?
>
> Chris
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---