gpt4 book ai didi

python - Django 通用 View ManyToMany 与 "through"模型

转载 作者:太空宇宙 更新时间:2023-11-03 16:45:03 24 4
gpt4 key购买 nike

我的第一个 Django 项目遇到了一些问题,这一定是一个常见/易于解决的问题!作为一些背景知识,我正在构建一个用于跟踪治疗预约的应用程序。大多数数据结构都相当简单,除了这个需要使用中间(直通)模型的多对多的实例。正如您在下面的 models.py 中看到的,有三个模型与我遇到的问题相关。 Contact 模型用于存储客户的联系方式。案例模型是处理一项工作/工作的概念。任何特定的案例都可以有多个 session ,等等。通常情况下,一个案例可能有两个或多个联系人,他们将分摊账单。因此,需要具有中介模型的多对多来存储该特定联系人将支付的账单的百分比。模型.py

class Contact(models.Model):
first_name = models.CharField(max_length=200)
last_name = models.CharField(max_length=200)
class Case(models.Model):
invoicees = models.ManyToManyField(Contact, through='Invoicees_Members', through_fields=('case','contact'),null=True, blank=True)
class Invoicees_Members(models.Model):
contact = models.ForeignKey(Contact, on_delete=models.CASCADE)
case = models.ForeignKey(Case, on_delete=models.CASCADE)
invoice_percentage = models.IntegerField(validators = [MinValueValidator(1), MaxValueValidator(100)],null=True, blank=True)

我在 stackoverflow 和其他网站上进行了大量搜索,了解如何处理保存表单提交。最常见的解决方案似乎是我在下面尝试实现的。您会注意到我正在尝试使用通用的基于类的 View (在本例中为 CreateView)。

View .py

class CaseCreate(CreateView):
model = Case
success_url = '/cases/'
fields = '__all__'
def form_valid(self, form):
self.instance = form.save(commit=False)
for contact in form.cleaned_data['invoicees']:
invoicee = Invoicees_Members()
invoicee.case = self.instance
invoicee.contact = contact
invoicee.save()
return super(ModelFormMixin, self).form_valid(form)

不幸的是,表单提交会导致以下错误:“异常值:禁止 save() 以防止由于未保存的相关对象“case”而导致数据丢失”。我的假设是,由于某种原因, form.save(commit=False) 不会返回用于 Invoicees_Members 模型保存的 ID...

有什么想法吗?我在这里犯了错误,这一定是一些微不足道的事情。附:我尝试使用 self.object 代替 self.instance 并遇到相同的错误。

错误和堆栈跟踪:

Traceback:

File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
149. response = self.process_exception_by_middleware(e, request)

File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
147. response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Python27\lib\site-packages\django\views\generic\base.py" in view
68. return self.dispatch(request, *args, **kwargs)

File "C:\Python27\lib\site-packages\django\views\generic\base.py" in dispatch
88. return handler(request, *args, **kwargs)

File "C:\Python27\lib\site-packages\django\views\generic\edit.py" in post
256. return super(BaseCreateView, self).post(request, *args, **kwargs)

File "C:\Python27\lib\site-packages\django\views\generic\edit.py" in post
222. return self.form_valid(form)

File "C:\Users\danie\Documents\django-projects\office_management\officeman\views.py" in form_valid
40. invoicee.save()

File "C:\Python27\lib\site-packages\django\db\models\base.py" in save
651. "unsaved related object '%s'." % field.name

Exception Type: ValueError at /cases/add
Exception Value: save() prohibited to prevent data loss due to unsaved related object 'case'.

最佳答案

可能是ManyToMany关系导致的保存顺序问题。最好先阅读官方文档......

Every time you save a form using commit=False, Django adds a save_m2m() method to your ModelForm subclass. After you’ve manually saved the instance produced by the form, you can invoke save_m2m() to save the many-to-many form data.

示例:

# Create a form instance with POST data.
>>> f = AuthorForm(request.POST)

# Create, but don't save the new author instance.
>>> new_author = f.save(commit=False)

# Modify the author in some way.
>>> new_author.some_field = 'some_value'

# Save the new instance.
>>> new_author.save()

# Now, save the many-to-many data for the form.
>>> f.save_m2m()

阅读带有示例的 save 方法的完整文档 Here

关于python - Django 通用 View ManyToMany 与 "through"模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36390247/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com