Когда я нажимаю «Сохранить» в представлении, создаются два экземпляра возможности. Единственная строка кода, которая создает что-либо в базе данных, это new_opp.save. Я новичок в этом и не могу понять, как сделать только один объект Opportunity в коде.
views.py
class OpportunityMenuCreate(CreateView):
model = Opportunity
template_name = 'OpportunityMenu.html'
fields = ['generalinformation']
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
opp = Opportunity.objects.get(pk=self.kwargs['pk'])
cur_opp = Current_Opportunity.objects.create()
cur_opp.captureinformation = opp.captureinformation.id
cur_opp.fundinginformation = opp.fundinginformation.id
cur_opp.generalinformation = opp.generalinformation.id
cur_opp.pointsofcontact = opp.pointsofcontact.id
cur_opp.requirementsinformation = opp.requirementsinformation.id
cur_opp.teamsratingsinformation = opp.teamsratingsinformation.id
cur_opp.save()
return context
forms.py
class InitializeForm (BSModalForm):
def __init__(self, *args, **kwargs):
super(InitializeForm, self).__init__(*args, **kwargs)
self.helper = FormHelper(self)
self.helper.layout = Layout(
Row(
Column('solicitation_name', css_class="container col-md-
6"),
),
Row(
Column('solicitation_ID', css_class="container col-md-6")
)
)
class Meta:
model = GeneralInfo
fields = ['solicitation_ID', 'solicitation_name']
urls.py
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from .views import home_page, about_page
urlpatterns = [
path('', home_page, name='home_page'),
path('Admin/', admin.site.urls),
path('Account/', include('Account.urls', namespace='account')),
path('DataElement/', include('DataElements.urls',
namespace='DataElements')),
path('Opportunities/', include('Opportunity.urls',
namespace="Opportunities")),
path('About/', about_page, name='about_page'),
]
if settings.DEBUG:
urlpatterns = urlpatterns + static(settings.STATIC_URL,
document_root=settings.STATIC_ROOT)
urlpatterns = urlpatterns + static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
html для формы
{% load crispy_forms_tags %}
<form method="post">
{% csrf_token %}
<div class="modal-header">
<h5 class="modal-title">Initialize New Opportunity</h5>
<button type="button" class="close" data-dismiss="modal" aria-
label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="form-group{% if field.errors %} invalid{% endif %}">
{% crispy form %}
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-
dismiss="modal">Close</button>
<button type="button" class="submit-btn btn btn-primary">Create</button>
</div>
</form>
Спасибо за любую помощь.