Models.py
class Centre(models.Model):
Ccode = models.AutoField(primary_key=True)
zone = models.CharField(max_length=4, blank=True, null=True)
centre = models.CharField(max_length=50, unique=True, blank=True, null=True)
def __str__(self):
return '%s %s' % (str(self.Ccode ), self.centre)
class SubCentre(models.Model):
Ccode = models.ForeignKey(Centre, blank=True, null=True, on_delete=models.SET_NULL, to_field="Ccode", related_name="Ccode")
zone = models.CharField(max_length=4, blank=True, null=True)
centre = models.ForeignKey(Centre, blank=True, null=True, on_delete=models.SET_NULL, to_field="centre", related_name="centre")
org = models.ForeignKey(org, blank=True, null=True, on_delete=models.SET_NULL, to_field="Org", db_column="org")
name = models.CharField(max_length=50, blank=True, null=True)
def __str__(self):
return str(self.name)
Views.py
def create_SCentre(request,pk):
centre = Centre.objects.get(pk=pk)
SubCentres = SubCentre.objects.filter(centre=centre)
#initial_dict = {'Ccode':centre.Ccode, 'zone':centre.zone, 'centre':centre.centre}
form = SubCentreForms(request.POST or None, instance=centre)
if request.method == "POST":
if form.is_valid():
SubCentre = form.save(commit=False)
SubCentre.centre = centre.centre
SubCentre.save()
return redirect('detail-SubCentre', pk=SubCentre.id)
else:
return render(request, 'partials/SubCentre_form.html', {
"form": form
})
context = {
"form": form,
"centre": centre,
"SubCentres": SubCentres
}
return render(request, 'create_SubCentre.html', context)
def create_SCentre_form(request,pk):
centre = Centre.objects.get(pk=pk)
form = SubCentreForms(instance=centre)
context = {
"form": form
}
return render(request, "partials/SubCentreform.html", context)
The fields from the model ‘Centre’ are autopopulated while creating data for the model ‘SubCentre’. But as shown in the below screenshot, the values are fetched together instead of individually.
How to bring the values individually?