无法获取父 ID 记录以在默认保存内联时保存额外记录

发布于 2024-11-19 12:03:52 字数 1498 浏览 0 评论 0原文

  • 我正在保存一个项目。
  • 它有一个实践者内联模型。
  • 在 Practitioner 模型的默认保存中,我正在运行自定义代码以将另一条记录插入到另一个名为 Simple Auth 的表中。

我的想法是,如果我将项目的实例传递给从业者,就像我在views.py中所做的那样,那么 def save 将能够引用 self 的项目。

所以我的views.py看起来像这样:

def form_valid(self, form):
    practitioner_form = context['practitioner_form']
    if practitioner_form.is_valid():
        self.object = form.save(commit=False)
        self.object.slug = slugify(self.object.title)
        self.object.save()
        practitioner_form.instance = self.object
        practitioner_form.save()
        return HttpResponseRedirect(reverse('profile_detail', kwargs={'username':user.username}))
    else:
        return self.render_to_response(self.get_context_data(form=form)) 

我的Practitioner def save看起来像这样:

def save(self, *args, **kwargs):
    if SimpleAuth.objects.filter(project=self.project,name=self.practitioner_name).exists():
        simpleauth = SimpleAuth.objects.get(project=self.project,name=self.practitioner_name)
        simpleauth.project = self.project
        simpleauth.name = self.practitioner_name
        simpleauth.save()
    else:
        SimpleAuth.objects.create(project = self.project, name = self.practitioner_name)
    super(Practitioner, self).save(*args, **kwargs)

出于某种原因 simpleauth.project = self.project 似乎没有任何值。为什么我不能像这样获取项目 id?

只是一个注释。如果我通过管理员创建我的项目,那么一切都会正常。根据我的表格,这是行不通的。

  • I'm saving a Project.
  • It has a Practitioner inline model.
  • On the def save of the Practitioner model I am running custom code to insert another record into another table called Simple Auth.

My thinking was that if I pass through the instance of the project to the practitioner like I'm doing in the views.py, that the def save would then be able to reference the project of of self.

So my views.py looks like this:

def form_valid(self, form):
    practitioner_form = context['practitioner_form']
    if practitioner_form.is_valid():
        self.object = form.save(commit=False)
        self.object.slug = slugify(self.object.title)
        self.object.save()
        practitioner_form.instance = self.object
        practitioner_form.save()
        return HttpResponseRedirect(reverse('profile_detail', kwargs={'username':user.username}))
    else:
        return self.render_to_response(self.get_context_data(form=form)) 

My Practitioner def save looks like this:

def save(self, *args, **kwargs):
    if SimpleAuth.objects.filter(project=self.project,name=self.practitioner_name).exists():
        simpleauth = SimpleAuth.objects.get(project=self.project,name=self.practitioner_name)
        simpleauth.project = self.project
        simpleauth.name = self.practitioner_name
        simpleauth.save()
    else:
        SimpleAuth.objects.create(project = self.project, name = self.practitioner_name)
    super(Practitioner, self).save(*args, **kwargs)

For some reason simpleauth.project = self.project doesn't seem to have any value. Why can't I get the project id like this?

Just a note. If I create my project via the admin then it all works. It is on my form that this doesn't work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月朦胧 2024-11-26 12:03:53

我不认为这是最好的方法,但目前它有效。如果没有人以更优雅的方式回答,那么我会将其标记为答案。

在 def 保存中,我添加了 get_object_or_404。

def save(self, *args, **kwargs):
    project = get_object_or_404(Project, pk=self.project_id)
    if SimpleAuth.objects.filter(project=self.project,name=self.practitioner_name).exists():
        simpleauth = SimpleAuth.objects.get(project=project,name=self.practitioner_name)
        simpleauth.project = project
        simpleauth.name = self.practitioner_name
        simpleauth.save()
    else:
        SimpleAuth.objects.create(project = project, name = self.practitioner_name)
    super(Practitioner, self).save(*args, **kwargs)

I don't think it's the best way but for now it works. If no one answers with a more elegant way then I'll mark this as the answer.

In the def save I've added a get_object_or_404.

def save(self, *args, **kwargs):
    project = get_object_or_404(Project, pk=self.project_id)
    if SimpleAuth.objects.filter(project=self.project,name=self.practitioner_name).exists():
        simpleauth = SimpleAuth.objects.get(project=project,name=self.practitioner_name)
        simpleauth.project = project
        simpleauth.name = self.practitioner_name
        simpleauth.save()
    else:
        SimpleAuth.objects.create(project = project, name = self.practitioner_name)
    super(Practitioner, self).save(*args, **kwargs)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文