B
B
Briariy2018-09-05 14:52:26
Django
Briariy, 2018-09-05 14:52:26

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)

There is forms.pu
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'})
        }

Have Veiw
class TaskCreateView(CreateView):
    model = Task
    form_class = TaskForm
    template_name = "task-create.html"
    success_url = '/'

And template
<form method="POST" action="">
{% csrf_token %}
{{from.author }} 
{{from.description }} 
{{from.comment }} 
</form>

Actually the question is how to make it possible to add a comment to the task
. I understand that the output {{from.comment }} is not correct, because in fact it is a select

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Arslan Aliev, 2018-09-05
@Arselon

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 question

Ask a Question

731 491 924 answers to any question