adding django admin many-to-many widget
In the django admin documentation, it says the following:
By default, admin widgets for many-to-many relations will be displayed on whichever model contains the actual reference to the ManyToManyField.
Is there a way to get a similar widget to appear on the admin page for the other model, the one w开发者_如何学JAVAhere the relationship isn't defined?
There's a couple different ways to get the effect that you're after.
Here's one way, which will get you a similar (but not identical) effect, and probably requires the least coding. (Examples will use classes A
and B
, assuming that the former has the many to many relationship explicitly defined)
The quickest way: you can use an InlineModelAdmin
object:
class AInline(admin.TabularInline):
model = A
class BAdmin(admin.ModelAdmin):
inlines = (AInline,)
admin.site.register(B, BAdmin)
If you want the exact effect of getting a <select multiple>
, the way you can do it is to use a custom Form
class, and assign it to BAdmin.form
.
精彩评论