Yousuf Jawwad
Yousuf Jawwad

Reputation: 3097

prepopulated field and function()

as per my understanding, pre-populated fields shouldn't include foriegnkey. i thought of a workaround.

in my model .. i defined a function by the name getname()

def getname(self):
    t1 = str(self.team_one)
    t2 = str(self.team_two)
    t1t2 = t1 + ' vs ' + t2
    return t1t2

and thought of calling it in my admin .. this is how i am doing it.

prepopulated_fields = { 'name': ('getname()',)}

it should solve my problem, but this is what django says.

Exception Type: ImproperlyConfigured
Exception Value:    
'FixtureAdmin.prepopulated_fields['name'][0]' refers to field 'getname' that is missing from model 'Fixture'.

is there a work around it, or should i drop the idea?

//mouse

Upvotes: 1

Views: 862

Answers (1)

Chris Pratt
Chris Pratt

Reputation: 239430

prepopulated_fields is only for slugs; it's not some generalized way to give a field a default value.

You'll have to write your own JavaScript for this. Something like the following should do the trick:

$('#id_team_one, #id_team_two').change(function(){
    var $team1 = $('#id_team_one');
    var $team2 = $('#id_team_two');
    if ($team1.val() && $team2.val()) {
        var team1_selected = $(':selected', $team1).text();
        var team2_selected = $(':selected', $team2).text();
        $('#id_name').val(team1_selected + ' vs ' + team2_selected);
    }
});

Upvotes: 1

Related Questions