Я пытаюсь перечислить города, которые зависят от выбранной страны. Я просматриваю примеры на документах. https://django-select2.readthedocs.io/en/latest/extra.html#chained -select2 Кажется, я не понимаю, как реализовать то, что на Docs. при попытке его реализации я получаю это сообщение об ошибке
TypeError at /page/
__init__() got an unexpected keyword argument 'instance'
Вот ошибка трассировки
Internal Server Error: /page/
Traceback (most recent call last):
File "C:\Users\Benedict\Miniconda3\envs\django2.1\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\Benedict\Miniconda3\envs\django2.1\lib\site-packages\django\core\handlers\base.py", line 115, in
_get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\Benedict\Miniconda3\envs\django2.1\lib\site-packages\django\core\handlers\base.py", line 113, in
_get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Benedict\Miniconda3\envs\django2.1\lib\site-packages\django\views\generic\base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\Benedict\Miniconda3\envs\django2.1\lib\site-packages\django\views\generic\base.py", line 97, in dispatch
return handler(request, *args, **kwargs)
File "C:\Users\Benedict\Miniconda3\envs\django2.1\lib\site-packages\django\views\generic\edit.py", line 168, in
get
return super().get(request, *args, **kwargs)
File "C:\Users\Benedict\Miniconda3\envs\django2.1\lib\site-packages\django\views\generic\edit.py", line 133, in
get
return self.render_to_response(self.get_context_data())
File "C:\Users\Benedict\Miniconda3\envs\django2.1\lib\site-packages\django\views\generic\edit.py", line 66, in get_context_data
kwargs['form'] = self.get_form()
File "C:\Users\Benedict\Miniconda3\envs\django2.1\lib\site-packages\django\views\generic\edit.py", line 33, in get_form
return form_class(**self.get_form_kwargs())
TypeError: __init__() got an unexpected keyword argument 'instance'
[19/Apr/2020 20:58:54] "GET /page/ HTTP/1.1" 500 94467
models.py
class Country(models.Model):
name = models.CharField(max_length=255)
class City(models.Model):
name = models.CharField(max_length=255)
country = models.ForeignKey('Country', related_name="cities", on_delete=models.CASCADE)
forms.py
from django import forms
from django_select2 import forms as s2forms
from .models import City, Country
class AddressForm(forms.Form):
country = forms.ModelChoiceField(
queryset=Country.objects.all(),
label=u"Country",
widget=s2forms.ModelSelect2Widget(
model=Country,
search_fields=['name__icontains'],
)
)
city = forms.ModelChoiceField(
queryset=City.objects.all(),
label=u"City",
widget=s2forms.ModelSelect2Widget(
model=City,
search_fields=['name__icontains'],
dependent_fields={'country': 'country'},
max_results=500,
)
)
views.py
from django.views.generic import ListView, CreateView, UpdateView
from django.urls import reverse_lazy
from .models import Country, City
from .forms import AddressForm
class PersonCreateView(CreateView):
model = City
form_class = AddressForm
success_url = reverse_lazy('person_changelist')
class PersonListView(ListView):
model = City
context_object_name = 'people'
template_name = 'django_starter_app/person_list.html'
urls.py
urlpatterns = [
path('', views.PersonCreateView.as_view(), name='person_add'),
]
person_form. html
<form method="post">
{{ form.as_p }}
<input type="submit" value="Save">
</form>