【发布时间】:2015-10-29 17:02:17
【问题描述】:
我正在尝试自定义我的管理员。我有一个模型与另一个模型的外键。我目前有一个下拉列表,显示可供选择的可能键列表,但列表仅显示标题。
模型.py
class Equipment(models.Model):
noequipment = models.IntegerField('Equipment #' ,db_column='Noequipment', primary_key=True) # Field name made lowercase.
nom = models.CharField('Name',db_column='Nom', max_length=50, blank=True) # Field name made lowercase.
...
nooffice = models.ForeignKey('Office', db_column='NoOffice', blank=True, null=True, verbose_name='Office') # Field name made lowercase.
...
class Meta:
db_table = 'equipment'
ordering = ('nom',)
def __str__(self):
return self.nom
class Office(models.Model):
nooffice = models.IntegerField(db_column='NoOffice', primary_key=True) # Field name made lowercase.
officename = models.CharField(db_column='OfficeName', max_length=50, blank=True) # Field name made lowercase.
adresse = models.CharField(db_column='Adresse', max_length=50, blank=True) # Field name made lowercase.
ville = models.CharField(db_column='Ville', max_length=50, blank=True) # Field name made lowercase.
codepostal = models.CharField(db_column='CodePostal', max_length=50, blank=True) # Field name made lowercase.
class Meta:
db_table = 'office'
def __str__(self):
return self.officename
我只需要管理员在下拉列表顶部显示一个包含属性值的表格。
这里也是 admin.py(需要特殊的 ModelAdmin,因为我使用多个数据库,根据 https://docs.djangoproject.com/en/1.8/topics/db/multi-db/)
from django.contrib import admin
from .models import Equipment, Manufacturier, Office, Devicetype, Backdoor, Passage, Systadmin, Applicationadmin, Application, Os
from django import forms
from forms import EquipmentAdminForm, ManufacturierAdminForm, OfficeAdminForm, DevicetypeAdminForm, BackdoorAdminForm, PassageAdminForm, SystadminAdminForm, ApplicationadminAdminForm, ApplicationAdminForm, OsAdminForm
class VCOEModelAdmin(admin.ModelAdmin):
# A handy constant for the name of the alternate database.
using = 'vcoe'
def save_model(self, request, obj, form, change):
# Tell Django to save objects to the 'other' database.
obj.save(using=self.using)
def delete_model(self, request, obj):
# Tell Django to delete objects from the 'other' database
obj.delete(using=self.using)
def get_queryset(self, request):
# Tell Django to look for objects on the 'other' database.
return super(VCOEModelAdmin, self).get_queryset(request).using(self.using)
def formfield_for_foreignkey(self, db_field, request=None, **kwargs):
# Tell Django to populate ForeignKey widgets using a query
# on the 'other' database.
return super(VCOEModelAdmin, self).formfield_for_foreignkey(db_field, request=request, using=self.using, **kwargs)
def formfield_for_manytomany(self, db_field, request=None, **kwargs):
# Tell Django to populate ManyToMany widgets using a query
# on the 'other' database.
return super(VCOEModelAdmin, self).formfield_for_manytomany(db_field, request=request, using=self.using, **kwargs)
class VCOETabularInline(admin.TabularInline):
using = 'vcoe'
def get_queryset(self, request):
# Tell Django to look for inline objects on the 'other' database.
return super(VCOEMTabularInline, self).get_queryset(request).using(self.using)
def formfield_for_foreignkey(self, db_field, request=None, **kwargs):
# Tell Django to populate ForeignKey widgets using a query
# on the 'other' database.
return super(VCOEMTabularInline, self).formfield_for_foreignkey(db_field, request=request, using=self.using, **kwargs)
def formfield_for_manytomany(self, db_field, request=None, **kwargs):
# Tell Django to populate ManyToMany widgets using a query
# on the 'other' database.
return super(VCOEMTabularInline, self).formfield_for_manytomany(db_field, request=request, using=self.using, **kwargs)
class OfficeAdmin(VCOEModelAdmin):
form = OfficeAdminForm
class EquipmentAdmin(VCOEModelAdmin):
form = EquipmentAdminForm
...
admin.site.register(Equipment, EquipmentAdmin)
...
admin.site.register(Office, OfficeAdmin)
...
和forms.py
from django import forms
from .models import Equipment, Manufacturier, Office, Devicetype, Backdoor, Passage, Systadmin, Applicationadmin, Application, Os
class EquipmentAdminForm(forms.ModelForm):
class Meta:
model = Equipment
exclude = ['noequipment']
...
class OfficeAdminForm(forms.ModelForm):
class Meta:
model = Office
exclude = ['nooffice']
【问题讨论】:
标签: django django-models django-admin