无法获取父 ID 记录以在默认保存内联时保存额外记录
- 我正在保存一个项目。
- 它有一个实践者内联模型。
- 在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不认为这是最好的方法,但目前它有效。如果没有人以更优雅的方式回答,那么我会将其标记为答案。
在 def 保存中,我添加了 get_object_or_404。
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.