Re: Limit choices based on a foreign key

2009-10-22 Thread Tim Valenta

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

Re: Limit choices based on a foreign key

2009-10-12 Thread Christian Wittwer

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



Re: Limit choices based on a foreign key

2009-10-12 Thread Peter Flood

It might not help your immediate problem but since players and teams
are the fundamental entities involved in team sports I'm sure you'll
need an easy way of linking them at some point.

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



Re: Limit choices based on a foreign key

2009-10-12 Thread Christian Wittwer

Hi Peter,
I forgot to post some important relations in the models.
GameRoster is a table in a m2m relation between Player and Game. I
need some extra attributes on the relationship between player and game
(which position they played).

class GameRoster(models.Model):
player = models.ForeignKey(Player)
game = models.ForeignKey(Game)
POSITION_CHOICES = (
('C', 'Coach'),
('G', 'Goalkeeper'),
('D', 'Defense'),
('O', 'Offense'),
)
position = models.CharField(max_length=1,choices=POSITION_CHOICES)

class Game(models.Model):
players = models.ManyToManyField(Player,
through='GameRoster',blank=True,null=True, related_name='players')

> Why not have
>
> class Player(models.Model):
>    lastname = models.CharField(max_length=60)
>    team = models.ForeignKey(Team)

That won't work, because I need to know which position the player
played at the game.
The position can change from game to game. That's the reason I used
the GameRoster.

Chris

> Christian Wittwer wrote:
>> 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 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
-~--~~~~--~~--~--~---



Re: Limit choices based on a foreign key

2009-10-12 Thread pe...@whywouldwe.com

Seems odd to connect Player to Team via Goal and GameRoster (only for 
hometeam, presumably you have away team as well) - what about players 
that don't score, don't you want those details as well? Maybe you show 
an edited version of your models.

Why not have

class Player(models.Model):
lastname = models.CharField(max_length=60)
team = models.ForeignKey(Team)


This way players, teams and goals are all tied closely. And you could rename 
GameRoster to PlayerGames or something a little more intuitive.


Hope this helps.


Christian Wittwer wrote:
> 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 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
-~--~~~~--~~--~--~---



Limit choices based on a foreign key

2009-10-12 Thread Christian Wittwer

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