Django моделей: объект 'ModelFormOptions' не имеет атрибута 'private_fields' - PullRequest
0 голосов
/ 11 июля 2020

Даже если в представлении на основе классов fields = '__all__'. Я все еще получаю эту ошибку

ПОЛНЫЙ КОД

forms.py

class UploadAlbum(forms.ModelForm):
    musicFiles = forms.FileField(widget=forms.ClearableFileInput(attrs={'multiple': True}))
    class Meta:
        model = Album
        exclude = ('music','date',)

просмотров. py

class Rcollection(LoginRequiredMixin,CreateView):
    model = UploadAlbum
    template_name = 'release/collection.html'
    fields = '__all__'

    def form_valid(self, form):
        # This method is called when valid form data has been POSTed.
        # It should return an HttpResponse.
        print(form)
        return super(Rcollection, self).form_valid(form)

ПОЛНЫЙ СТЭКТРЕЙС

kingsley@Number2-Ubuntu:~/Documents/RedRiver$ python3 manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
July 11, 2020 - 15:43:21
Django version 3.0.7, using settings 'RedRiver.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Internal Server Error: /app/creator/release/rcollection/
Traceback (most recent call last):
  File "/home/kingsley/.local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/kingsley/.local/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/kingsley/.local/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/kingsley/.local/lib/python3.6/site-packages/django/views/generic/base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/kingsley/.local/lib/python3.6/site-packages/django/contrib/auth/mixins.py", line 52, in dispatch
    return super().dispatch(request, *args, **kwargs)
  File "/home/kingsley/.local/lib/python3.6/site-packages/django/views/generic/base.py", line 97, in dispatch
    return handler(request, *args, **kwargs)
  File "/home/kingsley/.local/lib/python3.6/site-packages/django/views/generic/edit.py", line 168, in get
    return super().get(request, *args, **kwargs)
  File "/home/kingsley/.local/lib/python3.6/site-packages/django/views/generic/edit.py", line 133, in get
    return self.render_to_response(self.get_context_data())
  File "/home/kingsley/.local/lib/python3.6/site-packages/django/views/generic/edit.py", line 66, in get_context_data
    kwargs['form'] = self.get_form()
  File "/home/kingsley/.local/lib/python3.6/site-packages/django/views/generic/edit.py", line 32, in get_form
    form_class = self.get_form_class()
  File "/home/kingsley/.local/lib/python3.6/site-packages/django/views/generic/edit.py", line 101, in get_form_class
    return model_forms.modelform_factory(model, fields=self.fields)
  File "/home/kingsley/.local/lib/python3.6/site-packages/django/forms/models.py", line 554, in modelform_factory
    return type(form)(class_name, (form,), form_class_attrs)
  File "/home/kingsley/.local/lib/python3.6/site-packages/django/forms/models.py", line 257, in __new__
    apply_limit_choices_to=False,
  File "/home/kingsley/.local/lib/python3.6/site-packages/django/forms/models.py", line 144, in fields_for_model
    sortable_private_fields = [f for f in opts.private_fields if isinstance(f, ModelField)]
AttributeError: 'ModelFormOptions' object has no attribute 'private_fields'
[11/Jul/2020 15:43:26] "GET /app/creator/release/rcollection/ HTTP/1.1" 500 119649

1 Ответ

2 голосов
/ 11 июля 2020

Возможно, вы забыли указать атрибут формы в Rcollection.

form_class = UploadAlbum

И вам нужно изменить значение модели на Album.

model = Album
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...