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

标签 python django django-generic-views manytomanyfield

我的第一个 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/

相关文章:

python - 在 Python 中将 int 转换为字符串

python - 绘制直方图,其中 x 轴为索引值,y 轴为每列值的频率

python - 使用 python 从 s3 加载文件及其子文件夹

python - Django 基于类的 View : How do I pass additional parameters to the as_view method?

python - 如何使用通用 View 重定向登录用户查看 Django 中的登录页面

python - 如何在 Python 中将 Word 文档转换为非常简单的 html?

python - 保存 m2m 关系的 Django 问题

python - Django '<object> matching query does not exist' 当我在数据库中看到它时

python - 从 django.contrib.gis.geos 导入 GEOSException、GEOSGeometry、fromstr ImportError : cannot import name GEOSException

django-class-based-views - Django : How to call class-based generic views in views. py?