class IndexView(generic.ListView):
template_name = "posts/index.html"
def get_queryset(self):
return Inspectionfile.objects.all()
class DetailView(generic.DetailView):
model = Inspectionfile
template_name = "posts/detail.html"
class createposts(CreateView):
model = posts
fields = ['title','comments']
via the createposts and using forms I can populate the title and comments but they are not being linked with any Inspectionfile(the foreign key). I want the users to be linked without them having to choose. The following is my model. So I want to link every post to a particular inspectionfile.
class Inspectionfile(models.Model):
document_upload = models.FileField()
document_type = models.CharField(max_length=10)
document_title = models.CharField(max_length=250)
document_check = models.CharField(max_length=250)
def __str__(self):
return (self.document_title + self.document_type)
class posts(models.Model):
inspectionfile = models.ForeignKey(Inspectionfile, on_delete=models.CASCADE, default=1)
title = models.CharField(max_length=120)
comments = models.TextField()
flag = models.BooleanField(default=False)
def get_absolute_url(self):
return reverse('posts_form', kwargs={'pk': self.pk})
def __str__(self):
return self.title
form is a simple template:
<form class = "form_horizontal" action = "" method = "post">
{% csrf_token %}
{{form.as_p}}
<button type="submit">Submit</button>
</form>