Closed asnoeyink closed 7 years ago
You are probably right on this. After the refactoring for 1.0 I only used the unit tests for Form, since ModelForm fields where adjusted automatically. Since this since in 1.0 has changed to the way Django intends doing it, that mapping might have to be adjusted. So feel free to send me a pull request. Thanks for reporting this problem.
TL;DR: Django-Angular's field substitution doesn't work on model forms for
ModelChoiceField
, and usinggetattr()
causes an Attribute error for fields that don't have choices .Say I have a model and a form like this:
Django creates a form based off of the fields in SomeForm's
Meta
class. It chooses form fields equivalent to the model fields (models.CharField
->forms.CharField
andmodels.ForeignKey
->forms.ModelChoiceField
). Django-Angular is supposed to substitute these Django form fields for its own form fields fromdjng.forms.fields
. This works for all fields exceptModelChoiceField
, which causes an ImproperlyConfigured error.Currently, in
angular_base.py
,formfield_callback
is overridden to updateformfield()
with Django-Angular's new fields:This code caused two errors for me: I first had to change
getattr()
tohasattr()
in order to stop this code from hitting the database, which was causing any field that didn't have choices to error (AttributeError: 'CharField' object has no attribute 'choices'
).Then I was able to fix the ImproperlyConfigured error for
ModelChoiceFields
by removing theelse
. I think this fixes the issue because Django'sformfield()
checks if the field has any choices. Since it was no longer hitting the database, the field did not have choices (just an emptyModelChoiceIterator
, so Django skipped the block of code where it assignschoices_form_class
toform_class
. Removing theelse
allows Django-Angular'sModelChoiceField
to replace Django'sModelChoiceField
whether choices exist during the field's__init__
or not.So the lines above end up like this:
Is this the correct way to fix this, or am I misunderstanding the way either Django or Django-Angular handle this? I apologize in advance if any of this was unclear.