Opened 7 years ago

Closed 7 years ago

#26238 closed Cleanup/optimization (fixed)

confusing error message: django.core.exceptions.FieldError: Unknown field(s) specified when including a BinaryField in ModelForm.Meta.fields

Reported by: luke crouch Owned by: nobody
Component: Forms Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I had a Model with a CharField:

class PushApplication(models.Model):
    jws_key = models.CharField(blank=True, max_length=255)

I changed it to a BinaryField:

class PushApplication(models.Model):
    jws_key = models.BinaryField(blank=True)

When I tried to run makemigrations:

$ python manage.py makemigrations
...
django.core.exceptions.FieldError: Unknown field(s) (jws_key) specified for PushApplication

The problem was that I had a ModelForm with jws_key in the fields:

class PushAppForm(ModelForm):
    class Meta:
        fields = ['name', 'jws_key']

I removed jws_key from the ModelForm and it works.

But the error message sent me on an hour long web search trying to find out why changing a CharField to BinaryField would break migrations.

Change History (4)

comment:1 Changed 7 years ago by Simon Charette

Component: UncategorizedForms
Triage Stage: UnreviewedAccepted
Type: UncategorizedCleanup/optimization

Hi groovecoder,

In fact the exception you encountered has nothing to do with makemigrations; you should be able to also trigger it by simply importing the module defining the PushAppForm form class.

The reason behind the FieldError on PushAppForm creation is the fact BinaryField are not editable by default for obvious reasons and such fields are ignored by model forms.

I suggest we adjust the message of the FieldError raised when an existing editable=False field is explicitly specified through Meta.fields and amend the BinaryField's limitations documentation to also mention it can't be used in forms by default.

comment:2 Changed 7 years ago by Claude Paroz

Has patch: set
Version: 1.9master

comment:3 Changed 7 years ago by Simon Charette

Triage Stage: AcceptedReady for checkin

comment:4 Changed 7 years ago by Claude Paroz <claude@…>

Resolution: fixed
Status: newclosed

In d43156e1:

Fixed #26238 -- Raised explicit error for non-editable field in ModelForm

Thanks Luke Crouch for the report and Simon Charette for the review.

Note: See TracTickets for help on using tickets.
Back to Top