I did following:
add Media JS for User model (jquery and my own script follows), with
FireBug get the names of HTML id of the elemets I want to process.
Then created view with JSON output.
$(document).ready(function() {
$('#id_Port').html("");
$('#id_Switch').change(function() {
var switch_id = $('#id_Switch').val();
var ports = []
$.post("/cmdb/switch_ports/"+switch_id+"/", { "func":
"getNameAndTime" },
function(data){
for ( var i = 0; i < data.length; i++) {
var onePort = "<option value=\""+data[i].pk
+"\">"+data[i].fields.PortNum+"</option>";
$('#id_Port').append(onePort)
}
}, "json");
});
})
On 8 июн, 22:22, Dan Harris <[email protected]> wrote:
> Actually when I say not possible out of the box, I should really say
> without having to write some custom code. Since JQuery is built in,
> you should be able to write your own JQuery function that gets called
> when the switch drown down is changed
> (see:http://docs.djangoproject.com/en/dev/ref/contrib/admin/#modeladmin-me...).
> This function should make an ajax call to a custom view in your model
> admin
> (see:http://docs.djangoproject.com/en/dev/ref/contrib/admin/#adding-views-...).
>
> Overall you will have to write a javascript function (JQuery) and a
> custom view for your ModelAdmin. You of course don't have to use
> JQuery, but it is included by default in the admin and is quite
> popular.
>
> Dan Harris
> [email protected]
>
> On Jun 8, 3:17 pm, onorua <[email protected]> wrote:
>
>
>
> > Hello, thank you for your reply!
> > That's really what I've looked for, some way to prepopulate one drop-
> > box according to selected another one.
> > Crappy that it's impossible to do out of the box (would be great if it
> > could do).
>
> > Thank you for the hints about JQuery and 3rd party application, I'll
> > give it a try.
>
> > On 8 июн, 22:03, Dan Harris <[email protected]> wrote:
>
> > > Just trying to clarify your question a bit...
>
> > > Are you asking if there is a way in the admin interface that when a
> > > user selects a switch from a drop down box that the port drop down box
> > > will be automatically populated/filtered based on the selection in the
> > > switch drop down?
>
> > > If this is the case I don't believe that it is currently possible "out
> > > of the box" in the Django admin. Some work was being done to integrate
> > > JQuery into the admin for some Ajaxy stuff, but at the moment I don't
> > > think that very much found it's way in. There are probably a few 3rd
> > > party projects that implement this, but I don't know of any off the
> > > top of my head.
>
> > > If you already know what switch a user as selected (i.e. they saved it
> > > previously), then you can probaby populate the port list by overriding
> > > the formfield_for_foreign_key method in the AdminModel
> > > (see:http://docs.djangoproject.com/en/1.2/ref/contrib/admin/#modeladmin-me...)
>
> > > Hope this helps!
>
> > > Dan Harris
> > > [email protected]
>
> > > On Jun 8, 10:04 am, onorua <[email protected]> wrote:
>
> > > > I have
> > > > ====
> > > > class User(models.Model):
> > > > Switch = models.ForeignKey(Switch, related_name='SwitchUsers')
> > > > Port = models.ForeignKey(Port)
>
> > > > class Switch(models.Model):
> > > > Name = models.CharField(max_length=50)
>
> > > > class Port(models.Model):
> > > > PortNum = models.PositiveIntegerField()
> > > > Switch = models.ForeignKey(Switch, related_name = "Ports")
> > > > ====
>
> > > > I need when I create User or change User, and choose some Switch to
> > > > have Ports only related to that Switch in Option Box.
>
> > > > How can I acheave it?
> > > > Thank you.
--
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.