How to set the value of a field (which refers to the form, but is not based directly on the same model) in the form (model) when it is displayed in the admin panel?
Here is a simplified version of what I have (my actual application / model / etc is more complex):
- A
buildinghas many rooms - A
roomhas many partsequipment
Models:
from django.db import models
class Building(models.Model):
name=models.CharField(max_length=32)
def __unicode__(self):
return self.name
class Room(models.Model):
number=models.CharField(max_length=8)
building=models.ForeignKey(Building)
def __unicode__(self):
return self.number
class Equipment(models.Model):
tech = models.CharField(max_length=64)
room = models.ForeignKey(Room)
def __unicode__(self):
return self.tech
When adding or editing a piece of equipment, it would be nice to limit the selection to roomempty (by building). I can do this: I add a field buildingto ModelForm, and then when the user receives it, a little jQuery in the template file dynamically updates the list rooms. The item is admin.pyas follows:
from demo.spaces.models import Building, Room, Equipment
from django.contrib import admin
from django import forms
class EquipmentForm(forms.ModelForm):
class Meta:
model = Equipment
building = forms.ModelChoiceField(Building.objects)
class EquipmentAdmin(admin.ModelAdmin):
form = EquipmentForm
admin.site.register(Equipment, EquipmentAdmin)
admin.site.register(Building)
admin.site.register(Room)
equipment - JQuery ----- , , room room .
: (demo.com/admin/spaces/equipment/12/) , room room - building . equipment.room , room , building .
, init , , , :
def __init__(self, *args, **kwargs):
super(EquipmentForm, self).__init__(*args, **kwargs)
if 'instance' in kwargs:
building_value = kwargs['instance'].room.building
, ? , , , Jquery , , javascript , , , ...
? !