Answer the question
In order to leave comments, you need to log in
Creating a foreingkey from a model?
There was a problem with the form, I'm new to django so I'm sorry for the stupid question in advance
The essence of the problem
There are 2 models
class Task(models.Model):
author = models.ForeignKey('User', on_delete=models.DO_NOTHING)
data = models.DateTimeField(auto_now_add=True)
description = models.CharField(max_length=500)
comment = models.ForeignKey('TaskCommnet',on_delete=models.SET_NULL, blank=True, null=True)
class TaskComment(models.Model):
comment_date = models.DateTimeField(verbose_name='Дата комменатария', auto_now_add=True)
comment = models.CharField(verbose_name='Комментарий', max_length=300, blank=True, null=True)
class TaskForm(forms.ModelForm):
class Meta:
model = Task
fields = '__all__'
widgets = {
'author ': forms.Select(attrs={'class':'md-input form-control'}),
'description' :forms.Textarea(attrs={'class':'md-input form-control','rows':'4'}),
'comment' :forms.Textarea(attrs={'class':'md-input form-control','rows':'4'})
}
class TaskCreateView(CreateView):
model = Task
form_class = TaskForm
template_name = "task-create.html"
success_url = '/'
<form method="POST" action="">
{% csrf_token %}
{{from.author }}
{{from.description }}
{{from.comment }}
</form>
Answer the question
In order to leave comments, you need to log in
It is not clear why there is a separate table for comments, because You will have one unique comment for one task. I would do this:
class Task(models.Model):
author = models.ForeignKey('User', on_delete=models.DO_NOTHING)
data = models.DateTimeField(auto_now_add=True)
description = models.CharField(max_length=500)
comment_date = models.DateTimeField(verbose_name='Comment Date', blank=True, null=True)
comment = models.CharField(verbose_name='Comment', max_length=300, blank=True, null=True)
{% extends "app/ layout.html" %}
{% block content %}
form action="" method="post">
{{ form.as_table }}
/table>
input type="submit" value="Submit" />
/form>
{% endblock %}>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question