Reputation: 51
I want to send data from Form but can't send a specific data
for example: in my model has a student that I want send separate from view
in view:
student = Student.objects.filter(id=id)
if request.method == "POST":
form = StudentProject(request.POST, files=request.FILES)
form.student_id=id
form.save()
return redirect('main')
in form:
class Meta:
model=Project
fields=['name','link','image','body','term']
in model:
name=models.CharField(max_length=100,null=False)
link=models.CharField(max_length=1000,null=False)
image=models.ImageField(upload_to='static/project/images/')
body=models.TextField()
term=models.DecimalField(max_digits=1,decimal_places=0,null=False)
student=models.ForeignKey(Student,on_delete=models.CASCADE)
created_at=models.DateTimeField(default=timezone.now)
Upvotes: 1
Views: 47
Reputation: 51
I found that I can modify ModelForm
in django
like this:
class MyForm(forms.ModelForm):
name=forms.CharField(label="Name",widget=forms.TextInput(
attrs={
'class':'form-control',
}
))
Upvotes: 1
Reputation: 2383
If you want to send specific data, then exclude all the fields that you don't want to be on the form. Suppose I only want the name of the project:
class Meta:
model = Project
fields = ['name']
in my model has a student that I want send separate from view
I think the cleanest way to do this is to separate your initial form into two, create two views, and have the user input it separately.
Upvotes: 0