I would very much be interested in a solution to this problem.  I
can't yet understand why Django is so wonderful at so many things, yet
has absolutely no mechanism for this.  Every time there seems to be
hope of a working solution, it never works for inlines.

For the sake of painting up another example of what I understand to be
the same problem, I've got these models:

# models.py
class Company(models.Model):
    # ...
class Contract(models.Model):
    company = models.ForeignKey(Company)
    locations = models.ManyToManyField('Location')
class Location(models.Model):
    company = models.ForeignKey(Company)

# admin.py
class LocationInline(admin.TabularInline):
    model = Location
class ContractInline(admin.TabularInline):
    model = Contract
class CompanyAdmin(admin.ModelAdmin):
    inlines = (ContractInline, LocationInline)


This creates a problem when setting up the Admin for Company, because
it has inlines for both Contract and Location, and Contract's m2m
options for Location are not properly filtered according to the
Company that you're currently editing.

Is there anyone out there who knows of something straightforward to
satisfy this badly needed shortcut? Every "solution" I find doesn't
really deal with the Admin site, and it involves sketchy views that
try to use middleware to catch the object pk out of the URL.  totally
not ideal.

Back when I made PHP admins for this sort of thing, this was
considered basic functionality! In fact, it was always automatic, and
had to be disabled if you really didn't want it!

I've seen stranded unanswered posts on this subject for 3 years!  We
need a solution!  I just can't figure out anything that doesn't
require digging into the guts of the code and adding a new shortcut
API method on admin.ModelAdmin / TabularInline to help achieve this
goal.

Tim

On Oct 12, 9:36 am, Christian Wittwer <wittwe...@gmail.com> wrote:
> 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 theform..
>
> Any ideas who to activate my customformfor the inline mode?
>
> Chris
>
> 2009/10/12 Christian Wittwer <wittwe...@gmail.com>:
>
>
>
> > 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 inlineformto
> > 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 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