Custom field on admin.py - FieldDoesNotExist when using RelatedFieldWidgetWrapper

24 views
Skip to first unread message

Naran Khetani

unread,
May 8, 2019, 7:58:11 AM5/8/19
to Django users
Hello Users,
Hoping for someone to help me with this, I have a admin form which I am overwriting as below:

admin.py

def add_related_field_wrapper(form, col_name):
    rel_model
= form.Meta.model
    rel
= rel_model._meta.get_field(col_name).rel
    form
.fields[col_name].widget = RelatedFieldWidgetWrapper(
        form
.fields[col_name].widget,
        rel
,
        admin
.site,
        can_add_related
=True,
        can_change_related
=True
   
)


class DocumentItemsForm(forms.ModelForm):

    tags
= ModelMultipleChoiceField(label="Tags", required=False, queryset=Terms.objects.filter(termtypeid__name='tag'))
    categorys
= ModelMultipleChoiceField(label="Categorys", required=False, queryset=Terms.objects.filter(termtypeid__name='category'))

   
def __init__(self, *args, **kwargs):
       
super(DocumentItemsForm, self).__init__(*args, **kwargs)
        add_related_field_wrapper
(self, 'tags')

@admin.register(Documentitems)
class DocumentitemsAdmin(admin.ModelAdmin):

    form
= DocumentItemsForm
    save_as
= True


I get the below exception:

Django Version: 2.1.7
Exception Type: FieldDoesNotExist
Exception Value: Documentitems has no field named 'tags'

The actual model does not have those fields above, I have a feeling its looking up the actual model which is why its not finding the field. In essence what im trying to do is add a custom field that does not exist in the model which works fine, however i need to be able to add the plus button next to the field which allows the user to add a new record and ive be lead to the above solution to achieve this.
Reply all
Reply to author
Forward
0 new messages