如何保存django模型与manyToMany通过关系,和常规manyToMany关系 [英] How to save a django model with a manyToMany Through relationship, AND regular manyToMany relationships

查看:147
本文介绍了如何保存django模型与manyToMany通过关系,和常规manyToMany关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了在Django中的ManyToMany关系中可以找到的所有内容,但到目前为止,我没有看到任何解决我的具体问题,就像一个SimpleToMany一样简单的ManyToMany,Django自己处理这么简单



考虑这些模型:

  class Treatment(models.Model )
...
book = models.ManyToManyField(Book,through ='TreatmentLocation')
category = models.ManyToManyField(Category)

class Book
name = models.CharField(...)

class TreatmentLocation(models.Model):
treatment = models.ForeignKey(Treatment)
book = models.ForeignKey(Book)
page = models.CharField(...)

class类别(models.Model):
name = models.CharField(。 ..)

我有很好的数据进入POST阵列,但finagling视图证明是棘手的。

  def save_treatment(request):
form = TreatmentForm(request.POST)

如果form.is_valid():
treat = form.save()

pages = request.POST.getlist('page')
books = request.POST.getlist('book')

counter = 0

book_id = books .__ getitem __(counter)
TreatmentLocation.objects.create(treatment_id = treatment.id,book_id = book_id,page = page)
counter = counter + 1

form.save_m2m()
else:
...

治疗保存成功,就像TreatmentLocations一样,但是一旦我尝试调用save_m2m()来存储Treatment-Category外部参照,我得到不能在指定中间模型的ManyToManyField上设置值。使用TreatmentLocation Manager 错误。



有谁知道如何保存这两个东西?我想避免使用原始SQL。

解决方案

为什么不删除通过 ManyToManyField 从您的 ModelForm

  class MyForm(forms.ModelForm):
class Meta:
exclude =('book',)
model = Treatment


I've read everything I can find on ManyToMany relationships in Django, but so far I'm not seeing anything that solves my specific problem of having a ManyToMany-Through relationship alongside a simple ManyToMany that Django handles so effortlessly on its own.

Consider these models:

class Treatment(models.Model):
    ...
    book = models.ManyToManyField(Book, through='TreatmentLocation')
    category = models.ManyToManyField(Category)

class Book(models.Model):
    name = models.CharField(...)

class TreatmentLocation(models.Model):
    treatment = models.ForeignKey(Treatment)
    book = models.ForeignKey(Book)
    page = models.CharField(...)

class Category(models.Model):
    name = models.CharField(...)

I've got the data coming in on the POST array nicely, but finagling the View is proving tricky.

def save_treatment(request):
    form = TreatmentForm(request.POST)

    if form.is_valid():
        treatment = form.save()

        pages = request.POST.getlist('page')
        books = request.POST.getlist('book')

        counter = 0
        for page in pages:
            book_id = books.__getitem__(counter)
            TreatmentLocation.objects.create(treatment_id=treatment.id, book_id=book_id, page=page)
            counter = counter + 1

        form.save_m2m()
    else:
        ...

The Treatment saves successfully, as do the TreatmentLocations, but once I try to call save_m2m() to store the Treatment-Category xrefs, I get the Cannot set values on a ManyToManyField which specifies an intermediary model. Use TreatmentLocation Manager error.

Does anyone know how to save both of these things? I'd like to avoid resorting to raw SQL.

解决方案

Why don't you just remove the through ManyToManyField from your ModelForm?

class MyForm(forms.ModelForm):
    class Meta:
        exclude = ('book',)
        model = Treatment

这篇关于如何保存django模型与manyToMany通过关系,和常规manyToMany关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆