Friday, July 28, 2017

Problems with ModelForm which edits fields in 2 models

Hi friends,

I have a problem with a form in Django 1.11.3. Here is the form:

class ProfileNotificationsForm(forms.ModelForm):
class Meta:
model = User
fields = ('notify_on_message', )
profile_model = get_site_profile_model(profile_model=None)
profile_fields = ()

def __init__(self, **kwargs):
super().__init__(**kwargs)
for field in self._meta.profile_model._meta.fields:
if field.name in self._meta.profile_fields:
self.fields[field.name] = field.formfield()
self.helper = FormHelper()
self.helper.add_input(Submit('submit', pgettext_lazy(context=self.instance.get_gender(), message='Save Changes')))

def save(self, commit=True):
for field_name in self.fields.keys():
if field_name in self._meta.profile_fields:
setattr(self.instance.profile, field_name, self.cleaned_data[field_name])
return super().save(commit=commit)
This is the view:

class EditProfileNotificationsView(LoginRequiredMixin, FormValidMessageMixin, generic.UpdateView):
template_name = 'accounts/edit_profile/notifications.html'
success_url = reverse_lazy('accounts:edit_profile_notifications')
form_class = ProfileNotificationsForm

def get_object(self, queryset=None):
return self.request.user
In speedy.net this form edits only one field in one model. But in speedy.match I want to add another field from another model:

from speedy.core.accounts.forms import ProfileNotificationsForm as CoreProfileNotificationsForm
class ProfileNotificationsForm(CoreProfileNotificationsForm):
class Meta(CoreProfileNotificationsForm.Meta):
profile_fields = ("notify_on_like", )

And the view:

class EditProfileNotificationsView(CoreEditProfileNotificationsView):
form_class = ProfileNotificationsForm

But the problem is, this form throws an exception:

speedy\net>manage.py test
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
.....E.............................................................EE................................................................................................................................................................
======================================================================
ERROR: test_has_correct_fields (speedy.core.accounts.tests.test_forms.ProfileNotificationsFormTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "E:\Uri\Speedy Net\Git\speedy-net [public]\speedy\core\base\test.py", line 31, in inner
    return method_or_class(*args, **kwargs)
  File "E:\Uri\Speedy Net\Git\speedy-net [public]\speedy\core\accounts\tests\test_forms.py", line 111, in test_has_correct_fields
    form = ProfileNotificationsForm(instance=self.user)
  File "E:\Uri\Speedy Net\Git\speedy-net [public]\speedy\core\accounts\forms.py", line 171, in __init__
    for field in self._meta.profile_model._meta.fields:
AttributeError: 'ModelFormOptions' object has no attribute 'profile_model'

======================================================================
ERROR: test_user_can_open_the_page (speedy.core.accounts.tests.test_views.EditProfileNotificationsViewTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "E:\Uri\Speedy Net\Git\speedy-net [public]\speedy\core\accounts\tests\test_views.py", line 293, in test_user_can_open_the_page
    r = self.client.get(self.page_url)
  File "E:\Uri\Speedy Net\Git\speedy-net [public]\.venv\lib\site-packages\django\test\client.py", line 536, in get
    **extra)
  File "E:\Uri\Speedy Net\Git\speedy-net [public]\.venv\lib\site-packages\django\test\client.py", line 340, in get
    return self.generic('GET', path, secure=secure, **r)
  File "E:\Uri\Speedy Net\Git\speedy-net [public]\.venv\lib\site-packages\django\test\client.py", line 416, in generic
    return self.request(**r)
  File "E:\Uri\Speedy Net\Git\speedy-net [public]\.venv\lib\site-packages\django\test\client.py", line 501, in request
    six.reraise(*exc_info)
  File "E:\Uri\Speedy Net\Git\speedy-net [public]\.venv\lib\site-packages\django\utils\six.py", line 686, in reraise
    raise value
  File "E:\Uri\Speedy Net\Git\speedy-net [public]\.venv\lib\site-packages\django\core\handlers\exception.py", line 41, in inner
    response = get_response(request)
  File "E:\Uri\Speedy Net\Git\speedy-net [public]\.venv\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "E:\Uri\Speedy Net\Git\speedy-net [public]\.venv\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "E:\Uri\Speedy Net\Git\speedy-net [public]\.venv\lib\site-packages\django\views\generic\base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "E:\Uri\Speedy Net\Git\speedy-net [public]\.venv\lib\site-packages\django\contrib\auth\mixins.py", line 56, in dispatch
    return super(LoginRequiredMixin, self).dispatch(request, *args, **kwargs)
  File "E:\Uri\Speedy Net\Git\speedy-net [public]\.venv\lib\site-packages\django\views\generic\base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "E:\Uri\Speedy Net\Git\speedy-net [public]\.venv\lib\site-packages\django\views\generic\edit.py", line 236, in get
    return super(BaseUpdateView, self).get(request, *args, **kwargs)
  File "E:\Uri\Speedy Net\Git\speedy-net [public]\.venv\lib\site-packages\django\views\generic\edit.py", line 174, in get
    return self.render_to_response(self.get_context_data())
  File "E:\Uri\Speedy Net\Git\speedy-net [public]\.venv\lib\site-packages\django\views\generic\edit.py", line 93, in get_context_data
    kwargs['form'] = self.get_form()
  File "E:\Uri\Speedy Net\Git\speedy-net [public]\.venv\lib\site-packages\django\views\generic\edit.py", line 45, in get_form
    return form_class(**self.get_form_kwargs())
  File "E:\Uri\Speedy Net\Git\speedy-net [public]\speedy\core\accounts\forms.py", line 171, in __init__
    for field in self._meta.profile_model._meta.fields:
AttributeError: 'ModelFormOptions' object has no attribute 'profile_model'

======================================================================
ERROR: test_user_can_save_his_settings (speedy.core.accounts.tests.test_views.EditProfileNotificationsViewTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "E:\Uri\Speedy Net\Git\speedy-net [public]\speedy\core\base\test.py", line 31, in inner
    return method_or_class(*args, **kwargs)
  File "E:\Uri\Speedy Net\Git\speedy-net [public]\speedy\core\accounts\tests\test_views.py", line 303, in test_user_can_save_his_settings
    r = self.client.post(self.page_url, data)
  File "E:\Uri\Speedy Net\Git\speedy-net [public]\.venv\lib\site-packages\django\test\client.py", line 548, in post
    secure=secure, **extra)
  File "E:\Uri\Speedy Net\Git\speedy-net [public]\.venv\lib\site-packages\django\test\client.py", line 350, in post
    secure=secure, **extra)
  File "E:\Uri\Speedy Net\Git\speedy-net [public]\.venv\lib\site-packages\django\test\client.py", line 416, in generic
    return self.request(**r)
  File "E:\Uri\Speedy Net\Git\speedy-net [public]\.venv\lib\site-packages\django\test\client.py", line 501, in request
    six.reraise(*exc_info)
  File "E:\Uri\Speedy Net\Git\speedy-net [public]\.venv\lib\site-packages\django\utils\six.py", line 686, in reraise
    raise value
  File "E:\Uri\Speedy Net\Git\speedy-net [public]\.venv\lib\site-packages\django\core\handlers\exception.py", line 41, in inner
    response = get_response(request)
  File "E:\Uri\Speedy Net\Git\speedy-net [public]\.venv\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "E:\Uri\Speedy Net\Git\speedy-net [public]\.venv\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "E:\Uri\Speedy Net\Git\speedy-net [public]\.venv\lib\site-packages\django\views\generic\base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "E:\Uri\Speedy Net\Git\speedy-net [public]\.venv\lib\site-packages\django\contrib\auth\mixins.py", line 56, in dispatch
    return super(LoginRequiredMixin, self).dispatch(request, *args, **kwargs)
  File "E:\Uri\Speedy Net\Git\speedy-net [public]\.venv\lib\site-packages\django\views\generic\base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "E:\Uri\Speedy Net\Git\speedy-net [public]\.venv\lib\site-packages\django\views\generic\edit.py", line 240, in post
    return super(BaseUpdateView, self).post(request, *args, **kwargs)
  File "E:\Uri\Speedy Net\Git\speedy-net [public]\.venv\lib\site-packages\django\views\generic\edit.py", line 181, in post
    form = self.get_form()
  File "E:\Uri\Speedy Net\Git\speedy-net [public]\.venv\lib\site-packages\django\views\generic\edit.py", line 45, in get_form
    return form_class(**self.get_form_kwargs())
  File "E:\Uri\Speedy Net\Git\speedy-net [public]\speedy\core\accounts\forms.py", line 171, in __init__
    for field in self._meta.profile_model._meta.fields:
AttributeError: 'ModelFormOptions' object has no attribute 'profile_model'

----------------------------------------------------------------------
Ran 229 tests in 45.231s

FAILED (errors=3)
Destroying test database for alias 'default'...

What is the problem? Did I do something wrong? Is it not possible to add fields to a meta class?

https://github.com/urievenchen/speedy-net , code under "speedy" folder.


Thanks,
Uri.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAMQ2MsFVbguQHzrE_NbfRO4QnVXiF-HjNag-s%3DmJr0zOQez8nw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment