简体   繁体   English

m2m关系不会显示或保存在Django中

[英]m2m relations are not displayed or saved in django

class A(Model):
    to_b = ManyToManyField('B', blank=True, through='AtoB')

class B(Model):
    to_a = ManyToManyField('A', blank=True, through='AtoB')

class AtoB(Model):
    a =  ForeignKey('A', on_delete=CASCADE)
    b =  ForeignKey('B', on_delete=CASCADE)
    usr =  ForeignKey(settings.USER, on_delete=CASCADE)
    # some other fields

Im making a django application. 我正在制作django应用程序。
This is roughly equivalent to what i have in my models.py 这大致等于我在models.py中拥有的东西
I need m2m relation between A and B to go through another model because i need to store additional data there. 我需要A和B之间的m2m关系才能通过另一个模型,因为我需要在那里存储其他数据。
Now there is a problem - when i try to save instance of model A in my custom view, relations with B instances are not saved no matter whether i pick them or not. 现在有一个问题-当我尝试在自定义视图中保存模型A的实例时,无论是否选择它们,都不会保存与B实例的关系。 And when i go to http://127.0.0.1:8000/admin and try to create instance of A from there, i dont even see proper field (should be <select multiple> i guess) for selecting relations with B . 当我转到http://127.0.0.1:8000/admin并尝试从那里创建A的实例时,我什至看不到用于选择与B的关系的正确字段(应该是<select multiple> )。
Can someone please explain me why relations are not saved, and not even displayed in /admin? 有人可以解释一下为什么为什么不保存关系,甚至不在/ admin中显示关系吗?
Here is code roughly equivalent to what i have in views.py: 这是大致相当于我在views.py中的代码:

class Create(CreateView):
    model = None  # A or B
    template_name = 'something.html'

    def form_valid(self, form):
        self.object = form.save(commit=False)
        form.save()
        return HttpResponseRedirect(self.get_success_url())

in urls.py i specify additional arguments like this: views.Create.as_view(model=models.A, fields=['to_b']) 在urls.py中,我指定了其他类似这样的参数: views.Create.as_view(model=models.A, fields=['to_b'])

It is not going to work. 它不会起作用。 If you are using your own ManytoMany intermediary table, you have to manually manage and save the objects yourself. 如果使用自己的ManytoMany中间表,则必须自己手动管理和保存对象。 Using Django's builtin functions won't work. 使用Django的内置函数将无效。

Save Object A , then save Object B and then save the relation in your AtoB table (which is also an object). 保存Object A ,然后保存Object B ,然后将关系保存在AtoB表(也是一个对象)中。

a_to_b = AtoB.objects.create(a=object_a, b=object_b, user=self.request.user)
print(a_to_b)

[...] Note that if you are using an intermediate model for a many-to-many relationship, some of the related manager's methods are disabled, so some of these examples won't work with such models. [...]请注意,如果您对多对多关系使用中间模型,则会禁用某些相关管理器的方法,因此其中一些示例不适用于此类模型。

https://docs.djangoproject.com/en/1.10/topics/db/examples/many_to_many/ https://docs.djangoproject.com/en/1.10/topics/db/examples/many_to_many/

Your error is explained here: https://docs.djangoproject.com/en/1.10/topics/db/models/#intermediary-manytomany 您的错误在此处进行了说明: https : //docs.djangoproject.com/zh/1.10/topics/db/models/#intermediary-manytomany

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM