Skip to content

DisallowedModelAdminToField when clicking "Add" button for empty changelist #1717

@mdigruber

Description

@mdigruber

What version of Unfold are you using?

0.72

What version of Django are you using?

4.2.26

What browser are you using?

Brave v1.84.141

Did you checked changelog/commit history, if the bug is not already fixed?

Yes

Did you searched other issues, if the bug is not already fixed?

Yes

Did you checked documentation?

Yes

Are you able to replicate the bug in the demo site?

No

Repository with reproduced bug

Create minimal repository where it is possible to reproduce an error.

Describe your issue

When I try to add the first entry for a model in the admin using Unfold’s changelist view, I get a DisallowedModelAdminToField error instead of the usual “add” form.

Model

My model is named FrequentlyAskedQuestion:

class FrequentlyAskedQuestion(models.Model):
    question = models.CharField(max_length=255)
    answer = models.TextField()
    # ... nothing special here (no FK with to_field etc.)

Registered in admin with Unfold:

from unfold.admin import ModelAdmin

@admin.register(FrequentlyAskedQuestion)
class FrequentlyAskedQuestionAdmin(ModelAdmin):
    pass

Steps to reproduce

Open the Django admin.

Navigate to the Frequently Asked Question changelist.

At this point there are no objects yet, so the Unfold empty state is shown:
“No results found – This page yielded into no results. Create a new item or reset your filters.”

Click the button + Frequently Asked Question hinzufügen (the orange “Add” button in the empty-state view).

Instead of the add form, Django raises an exception.

Actual behavior

Django raises:

DisallowedModelAdminToField at /admin/portalviews/frequentlyaskedquestion/add/
The field << MISSING VARIABLE "to_field" >> cannot be referenced.

The request URL contains a strange _to_field parameter:

/admin/portalviews/frequentlyaskedquestion/add/
?_popup=1&_to_field=%3C%3C+MISSING+VARIABLE+%22to_field%22+%3E

So it looks like the template that renders the “Add” button is outputting a missing template variable {{ to_field }} into the query string, and Django then treats this as to_field="<< MISSING VARIABLE "to_field" >>", which causes DisallowedModelAdminToField.

Stacktrace (top part):

DisallowedModelAdminToField at /admin/portalviews/frequentlyaskedquestion/add/
The field << MISSING VARIABLE "to_field" >> cannot be referenced.

Django Version: 4.2.26
Exception Type: DisallowedModelAdminToField
Request Method: GET
Request URL: http://localhost:8082/admin/portalviews/frequentlyaskedquestion/add/?_popup=1&_to_field=%3C%3C+MISSING+VARIABLE+%22to_field%22+%3E
Raised during: django.contrib.admin.options.add_view
...

Expected behavior

Clicking the “Frequently Asked Question hinzufügen” button on the empty changelist should open the usual add form at:

/admin/portalviews/frequentlyaskedquestion/add/

without adding any _to_field query parameter (or at least with a valid value), and without raising DisallowedModelAdminToField.

Additional context

This only happens when clicking the orange “Add” button in the Unfold empty-state view (see attached screenshots).

The model itself doesn’t use to_field or any unusual foreign key configuration.

If I manually visit /admin/portalviews/frequentlyaskedquestion/add/ without the _to_field parameter, the form renders correctly and I can create objects normally.

Screenshots are attached showing:

  1. The exception detail page with DisallowedModelAdminToField.
Image
  1. The empty changelist with the “Frequently Asked Question hinzufügen” button that triggers the error.
Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions