0
我是django的新手。Django通過外鍵形成
我需要從請求中獲取id,並且在創建表單時,我想讓它從此值中創建indintid字段的值,並從表單本身中休息。
models.py
class Complaint(models.Model):
complaintid= models.AutoField(primary_key=True)
dateofcomplaint=models.DateTimeField(auto_now=False, auto_now_add=True)
content = models.TextField()
policestation= models.CharField(max_length=120)
location=models.TextField()
def __unicode__(self):
return unicode(self.complaintid)
def __str__(self):
return self.complaintid
def get_absolute_url(self):
return reverse("crimefiles:detail", kwargs={"id":self.complaintid})
class Fir(models.Model):
firid=models.CharField(max_length=10,primary_key=True)
complaintid=models.ForeignKey(Complaint,default=None)
signedby= models.CharField(max_length=20)
content=models.TextField(default="First Information Report")
def __unicode__(self):
return self.firid
def __str__(self):
return self.firid
views.py
def fir_create(request,id=None):
form =FirForm(request.POST or None)
if form.is_valid():
instance=form.save(commit=False)
instance.save()
messages.success(request,"sucessfully Created")
return HttpResponseRedirect()
context={
"form":form
}
return render(request,"fir_form.html",context)
forms.py
class FirForm(forms.ModelForm):
class Meta:
model=Fir
fields=[
"firid",
"signedby",
"content"]
你的問題是? – Selcuk
我想從url中添加投訴ID並將其設置爲表單,並將其餘的值形式在fir_create函數im傳遞id中的fir形式..我想用它來保存爲投訴ID –