【问题标题】:Django Saving form with two foreign keys带有两个外键的 Django 保存表单
【发布时间】:2022-01-22 07:22:41
【问题描述】:

我正在尝试保存一个表单,该表单通过外键(项目注释)链接到另一个模型(项目注释)的数据(项目注释 cmets)。项目注释通过外键链接到另一个模型(项目)。我以为我只需要考虑直接关系(项目说明)。但是从我得到的错误来看,我还需要处理从项目注释到项目的关系。

错误:

IntegrityError at /projects/note/1/add_project_note_comment/
insert or update on table "company_project_projectnotes" violates foreign key constraint "company_project_proj_project_id_478f433c_fk_company_p"
DETAIL:  Key (project_id)=(0) is not present in table "company_project_project".

模型:

class Project(models.Model):
    title = models.CharField(max_length= 200)
    description = tinymce_models.HTMLField()

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse ('project_detail', args=[str(self.id)])

class ProjectNotes(models.Model):
    title = models.CharField(max_length=200)
    body = tinymce_models.HTMLField()
    date = models.DateField(auto_now_add=True)
    project = models.ForeignKey(Project, default=0, blank=True, on_delete=models.CASCADE, related_name='notes')

    def __str__(self):
        return self.title

class ProjectNoteComments(models.Model):
    body = tinymce_models.HTMLField()
    date = models.DateField(auto_now_add=True)
    projectnote = models.ForeignKey(ProjectNotes, default=0, blank=True, on_delete=models.CASCADE, related_name='notes')

观点:

class ProjectNotesCommentCreateView(CreateView):
    model = ProjectNotes
    template_name = 'company_accounts/add_project_note_comment.html'
    fields = ['body']

    def form_valid(self, form):
        projectnote = get_object_or_404(ProjectNotes, id=self.kwargs.get('pk'))
        comment = form.save(commit=False)
        comment.projectnote = projectnote
        comment.save()
        return super().form_valid(form)

    def get_success_url(self):
        return reverse('project_detail', args=[self.kwargs.get('pk')])

网址格式:

path('note/<int:pk>/add_project_note_comment/', ProjectNotesCommentCreateView.as_view(), name='add_project_note_comment'),

模板:

{% extends 'base.html' %}
{% load crispy_forms_tags %}

  {% block content %}
  <h1>Add Comment</h1>
  
   <form action="" method="post">
    {% csrf_token %}
    {{ form.media }}
    {{ form|crispy }}
    <input type="submit" value="save">
   </form>
  
  {% endblock content %}

关于如何让它发挥作用的任何想法?

【问题讨论】:

    标签: django django-views


    【解决方案1】:

    您不会与 ProjectNoteCommentspk 字段与 ProjectNote model 建立关系,并且两个模型的相关名称相同,您可能需要解决此问题。

    此外,您只是延迟提交表单ProjectNote,但您还必须通过向后引用projectnote__project 来处理Project 模型(相关名称可能会在此位置引起问题。

    【讨论】:

    • 感谢您收看 Rifat。我使相关名称独一无二。我仍然不确定如何处理对 Project 模型的引用。
    • @Finn comment.projectnote.project 应该访问您的 ProjectNote 模型中的项目字段
    • Rifat,关于如何实现它有什么建议吗?我是 Django/python 的新手。我在视图中尝试了几件事,但到目前为止,当我尝试保存表单时仍然出现错误。
    • 你可以分享你正在使用的模板吗?
    • 这里是模板:{% extends 'base.html' %} {% load crispy_forms_tags %} {% block content %} &lt;h1&gt;Add Comment&lt;/h1&gt; &lt;form action="" method="post"&gt; {% csrf_token %} {{ form.media }} {{ form|crispy }} &lt;input type="submit" value="save"&gt; &lt;/form&gt; {% endblock content %}
    猜你喜欢
    • 2017-08-31
    • 1970-01-01
    • 2010-11-15
    • 2015-12-21
    • 2013-10-11
    • 1970-01-01
    • 2019-08-17
    相关资源
    最近更新 更多