In my models.py i have something like:
在我的模型。我有一些类似的东西:
class LocationGroup(models.Model):
name = models.CharField(max_length=200)
class Report(models.Model):
name = models.CharField(max_length=200)
locationgroups = models.ManyToManyField(LocationGroup)
admin.py (standard):
管理。py(标准):
admin.site.register(LocationGroup)
admin.site.register(Report)
When I enter the admin page for Report, it shows a nice multiple choice field. How can I add the same multiple choice field in LocationGroup? I can access all Reports by calling LocationGroup.report_set.all()
当我为报告输入管理页面时,它显示了一个不错的多选择字段。如何在LocationGroup中添加相同的多选择字段?我可以通过调用LocationGroup.report_set.all()访问所有报告
3 个解决方案
#1
8
The workaround I found was to follow the instructions for ManyToManyFields with intermediary models. Even though you're not using the 'through' model feature, just pretend as if you were and create a stub model with the necessary ForeignKey.
我发现的解决方法是使用中间模型来遵循许多tomanyfields的说明。即使您没有使用“通过”模型特性,也可以假装使用了,并使用必要的ForeignKey创建存根模型。
# models: make sure the naming convention matches what ManyToManyField would create
class Report_LocationGroups(models.Model):
locationgroup = models.ForeignKey(LocationGroup)
report = models.ForeignKey(Report)
# admin
class ReportInline(admin.TabularInline):
model = models.Report_LocationGroups
class LocationGroupAdmin(admin.ModelAdmin):
inlines = ReportInline,
#2
2
I think yon can combine this sample code (source) wich breaks sync_db
我认为您可以将这个示例代码(源代码)与sync_db相结合
class ItemType(meta.Model):
name = meta.CharField(maxlength=100)
description = meta.CharField(maxlength=250)
properties = meta.ManyToManyField('PropertyType',
db_table='app_propertytype_itemtypes')
class PropertyType(meta.Model):
name = meta.CharField(maxlength=100)
itemtypes = meta.ManyToManyField(ItemType)
with this snippet
这个片段
class ManyToManyField_NoSyncdb(models.ManyToManyField):
def __init__(self, *args, **kwargs):
super(ManyToManyField_NoSyncdb, self).__init__(*args, **kwargs)
self.creates_table = False
to obtain something like
获得类似
class ItemType(meta.Model):
name = meta.CharField(maxlength=100)
description = meta.CharField(maxlength=250)
properties = meta.ManyToManyField_NoSyncdb('PropertyType',
db_table='app_propertytype_itemtypes')
class PropertyType(meta.Model):
name = meta.CharField(maxlength=100)
itemtypes = meta.ManyToManyField(ItemType)
Disclaimer : this is just a rough idea
免责声明:这只是一个粗略的想法。
Edit: There is probably someting to do with Django's 1.1 Proxy Models
编辑:这可能与Django的1.1代理模型有关
#3
1
I think what are you are looking for is admin inlines. In your admin.py you will want to add something like this:
我想你要找的是管理inlines。在你的管理。py,你想添加如下内容:
class LocationGroupInline(admin.TabularInline):
model = LocationGroup
class ReportAdmin(admin.ModelAdmin):
inlines = [ LocationGroupInline, ]
admin.site.register(Report, ReportAdmin)
admin.site.register(LocationGroup)
There are many options to include in LocationGroupInline if you want to further configure the inline display of the related model. Two of these options are form and formset, which will let you use custom Django Form and FormSet classes to further customize the look and feel of the inline model admin. Using this you can create a simple Form that displays just the multiple choice field you want (except for a M2M field it will not be possible to display as a single drop down, but a multiple select box). For example:
如果您想进一步配置相关模型的内联显示,可以在LocationGroupInline中包含许多选项。其中两个选项是form和formset,可以使用定制的Django form和formset类来进一步定制内联模型管理的外观和感觉。使用这个方法,您可以创建一个简单的表单,它只显示您想要的多个选择字段(除了一个M2M字段,它不可能显示为单个的下拉,而是一个多个选择框)。例如:
class MyLocationGroupForm(forms.Form):
location = forms.MultipleModelChoiceField(
queryset=LocationGroup.objects.all())
class LocationGroupInline(admin.TabularInline):
model = LocationGroup
form = MyLocationGroupForm
#1
8
The workaround I found was to follow the instructions for ManyToManyFields with intermediary models. Even though you're not using the 'through' model feature, just pretend as if you were and create a stub model with the necessary ForeignKey.
我发现的解决方法是使用中间模型来遵循许多tomanyfields的说明。即使您没有使用“通过”模型特性,也可以假装使用了,并使用必要的ForeignKey创建存根模型。
# models: make sure the naming convention matches what ManyToManyField would create
class Report_LocationGroups(models.Model):
locationgroup = models.ForeignKey(LocationGroup)
report = models.ForeignKey(Report)
# admin
class ReportInline(admin.TabularInline):
model = models.Report_LocationGroups
class LocationGroupAdmin(admin.ModelAdmin):
inlines = ReportInline,
#2
2
I think yon can combine this sample code (source) wich breaks sync_db
我认为您可以将这个示例代码(源代码)与sync_db相结合
class ItemType(meta.Model):
name = meta.CharField(maxlength=100)
description = meta.CharField(maxlength=250)
properties = meta.ManyToManyField('PropertyType',
db_table='app_propertytype_itemtypes')
class PropertyType(meta.Model):
name = meta.CharField(maxlength=100)
itemtypes = meta.ManyToManyField(ItemType)
with this snippet
这个片段
class ManyToManyField_NoSyncdb(models.ManyToManyField):
def __init__(self, *args, **kwargs):
super(ManyToManyField_NoSyncdb, self).__init__(*args, **kwargs)
self.creates_table = False
to obtain something like
获得类似
class ItemType(meta.Model):
name = meta.CharField(maxlength=100)
description = meta.CharField(maxlength=250)
properties = meta.ManyToManyField_NoSyncdb('PropertyType',
db_table='app_propertytype_itemtypes')
class PropertyType(meta.Model):
name = meta.CharField(maxlength=100)
itemtypes = meta.ManyToManyField(ItemType)
Disclaimer : this is just a rough idea
免责声明:这只是一个粗略的想法。
Edit: There is probably someting to do with Django's 1.1 Proxy Models
编辑:这可能与Django的1.1代理模型有关
#3
1
I think what are you are looking for is admin inlines. In your admin.py you will want to add something like this:
我想你要找的是管理inlines。在你的管理。py,你想添加如下内容:
class LocationGroupInline(admin.TabularInline):
model = LocationGroup
class ReportAdmin(admin.ModelAdmin):
inlines = [ LocationGroupInline, ]
admin.site.register(Report, ReportAdmin)
admin.site.register(LocationGroup)
There are many options to include in LocationGroupInline if you want to further configure the inline display of the related model. Two of these options are form and formset, which will let you use custom Django Form and FormSet classes to further customize the look and feel of the inline model admin. Using this you can create a simple Form that displays just the multiple choice field you want (except for a M2M field it will not be possible to display as a single drop down, but a multiple select box). For example:
如果您想进一步配置相关模型的内联显示,可以在LocationGroupInline中包含许多选项。其中两个选项是form和formset,可以使用定制的Django form和formset类来进一步定制内联模型管理的外观和感觉。使用这个方法,您可以创建一个简单的表单,它只显示您想要的多个选择字段(除了一个M2M字段,它不可能显示为单个的下拉,而是一个多个选择框)。例如:
class MyLocationGroupForm(forms.Form):
location = forms.MultipleModelChoiceField(
queryset=LocationGroup.objects.all())
class LocationGroupInline(admin.TabularInline):
model = LocationGroup
form = MyLocationGroupForm