我创建了一个表单集来提交项目成本,并且我希望每个提交都具有唯一的ID。我想稍后在URL中使用提交ID(即"projects/1/cost-submission-edit/3
“)。1是项目的ID,3应该是成本提交的ID。
该表单集定义为:
models.py
class ProjectData(models.Model):
project_name = models.CharField(max_digits = 20)
client = models.CharField(max_digits= 15)
class ProjectCostSubmission(models.Model):
project_name = models.ForeignKey(ProjectData, max_digits = 20)
cost_name = models.CharField(max_digits= 15)
amount = models.DecimalField(max_digits=9)
submission_date = models.DateField(auto_now_add=True)
payment_approval_date = models.DateField(auto_add_now=True)
forms.py
class MyForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.label_suffix = ""
class Meta:
model = ProjectData
fields = "__all__"
MyFormSet = inlineformset_factory(ProjectData,ProjectCostSubmission, form=MyForm,extra=2)
views.py
class ProjectCostView(CreateView):
template_name = "/project_cost.html"
model = ProjectCostSubmission
fields = '__all__'
form_class = MyForm
urls.py
path("/projects/<int:project_id>/cost-submission-edit/",ProjectCostView.as_view(),name="costView")
提交3次后,表单集编辑页面将如下所示呈现表单集:
Project Name Client Cost Name Amount Submission Date Approval Date
------------ ------ -------- ------ --------------- -------------
Project1 Client1 Cost A 1000.00 12/02/2019 12/03/2019
Project1 Client1 Cost B 1000.00 12/02/2019 12/03/2019
Project1 Client1 Cost C 1000.00 12/03/2019 12/03/2019
Project1 Client1 Cost A 1000.00 12/03/2019 12/03/2019
Project1 Client1 Cost F 1000.00 12/04/2019 12/04/2019
Project1 Client1 Cost G 1000.00 12/04/2019 12/04/2019
-------------------------------------------------------------------------
Total: $6000.00
-------------------------------------------------------------------------
with the url "/projects/1/cost-submission-edit/".'/1/' is the project id.
相反,我希望它看起来像这样:
url "/projects/1/cost-submission-edit/1/"
Project Name Client Cost Name Amount Submission Date Approval Date
------------ ------ -------- ------ --------------- -------------
Project1 Client1 Cost A 1000.00 12/02/2019 12/03/2019
Project1 Client1 Cost B 1000.00 12/02/2019 12/03/2019
-------------------------------------------------------------------------
Total: $2000.00
-------------------------------------------------------------------------
url "/projects/1/cost-submission/2/"
Project Name Client Cost Name Amount Submission Date Approval Date
------------ ------ -------- ------ --------------- -------------
Project1 Client1 Cost C 1000.00 12/03/2019 12/03/2019
Project1 Client1 Cost A 1000.00 12/03/2019 12/03/2019
-------------------------------------------------------------------------
Total: $2000.00
-------------------------------------------------------------------------
url "/projects/1/cost-submission/3/"
Project Name Client Cost Name Amount Submission Date Approval Date
------------ ------ -------- ------ --------------- -------------
Project1 Client1 Cost F 1000.00 12/04/2019 12/04/2019
Project1 Client1 Cost G 1000.00 12/04/2019 12/04/2019
-------------------------------------------------------------------------
Total: $2000.00
-------------------------------------------------------------------------
在这里,每个提交都具有唯一的ID,并将网址路径用作path("/projects/<int:project_id>/cost-submission-edit/",ProjectCostView.as_view(),name="costView")
如何为每个提交创建唯一的ID,以便我可以选择和编辑每个提交?谢谢。
答案 0 :(得分:0)
我认为您的问题与两个因素有关:
您的网址应具有两个动态值。
如果您希望该网址为"/projects/1/cost-submission-edit/1/"
,则您的网址应类似于:
path("/projects/<int:project_id>/cost-submission-edit/<int:submission_id>/",ProjectCostView.as_view(),name="costView")
您没有将url的动态值传递给视图。:
您的视图需要接收url的动态值。像这样:
def viewname(request, project_id,submission_id ):
#from here your view already knows for example what to look for in project
#1, subbmission 3