campo y función prepoblados ()

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?

//ratón

preguntado el 09 de marzo de 12 a las 17:03

What are you trying to do? Maybe you can just write your own custom Javscript using jQuery? -

i am trying to set the name of the fixture based on the two teams. -

1 Respuestas

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);
    }
});

respondido 09 mar '12, 18:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.