So I have solved the problem - one needs to create a form and a custom save method:
class MyModelForm(forms.ModelForm):
actor = Person.objects.filter(role='actor')
writer = Person.objects.filter(role='writer')
director = Person.objects.filter(role='director')
actors = forms.ModelMultipleChoiceField(widget=forms.SelectMultiple, queryset=actor, required=False)
writers = forms.ModelMultipleChoiceField(widget=forms.SelectMultiple, queryset=writer, required=False)
directors = forms.ModelMultipleChoiceField(widget=forms.SelectMultiple, queryset=director, required=False)
class Meta:
model = FilmWork
fields = '__all__'
@admin.register(FilmWork)
class FilmworkAdmin(admin.ModelAdmin):
form = MyModelForm
fieldsets = (
(None, {
'fields': ('title', 'plot', 'ratings', 'film_creation_date', 'age_limit', 'link', 'actors', 'writers', 'directors'),
}),
)
def save_model(self, request, obj, form, change):
obj.save()
q1 = form.cleaned_data.get('actors', None)
q2 = form.cleaned_data.get('writers', None)
q3 = form.cleaned_data.get('directors', None)
q1 = q1.union(q2).union(q3)
obj.person_set.set(q1)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…