How to replace content_type and object_id fields by a field with actual object in admin inline?

后端 未结 1 1619
南旧
南旧 2021-01-21 22:30

I have inline, which shows data of contenttype model, so instead of real objects, I see content_type and object_id fields. I can exc

1条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-21 22:55

    Found solution.

    Add form to admin.TabularInline:

    class CriteriaPlacesInlineAdmin(admin.TabularInline):
        model = PlacesToCriterias
        form = CriteriaPlacesChoicesFieldForm  # <- ADDED FORM
    
    class CriteriasAdmin(admin.ModelAdmin):
        inlines = [CriteriaPlacesInlineAdmin]
    
    admin.site.register(Criterias, CriteriasAdmin)
    

    Form:

    class CriteriaPlacesChoicesFieldForm(forms.ModelForm):
        ct_place_type = ContentType.objects.get_for_model(PlaceTypesGroups)
    
        object_id = forms.ModelChoiceField(PlaceTypesGroups.objects.all(), label='places')
        content_type = forms.ModelChoiceField(ContentType.objects.all(), initial=ct_place_type, widget=forms.HiddenInput())
    
        def clean_object_id(self):
            return self.cleaned_data['object_id'].pk
    
        def clean_content_type(self):
            return self.ct_place_type
    

    0 讨论(0)
提交回复
热议问题