访问多对多“通过"Formsets 中的关系字段 [英] Accessing Many to Many "through" relation fields in Formsets

查看:19
本文介绍了访问多对多“通过"Formsets 中的关系字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模型:

class End_User(models.Model):
    location = models.ForeignKey(Location) 
    first_name = models.CharField(max_length=70, blank=True, null=True)
    email_address = models.CharField(max_length=70, blank=True, null=True)

class Phone_Client(models.Model):
    end_user = models.ManyToManyField(End_User)
...
    extensions = models.CharField(max_length=20)

class Line(models.Model):
    phone_client = models.ManyToManyField(Phone_Client, through='Phone_Line' )
    ....
    voicemail = models.BooleanField(default=False)  

class Phone_Line(models.Model):
    phone_client = models.ForeignKey(Phone_Client)
    line = models.ForeignKey(Line)
    line_index = models.IntegerField()

所以基本上一个最终用户可以有很多电话,一部电话可以有很多线路,通过 Phone_line 关联.

So basically One end user can have many phones, a phone can have many Lines, related through Phone_line.

我的页面需要在同一页面中为 Phone_Clients 和 Line 创建所有这些对象可编辑和新实例创建的运行时.目前我正在为 Phone_Client 和 Lines 创建一个简单的 End_User 模型表单和 modelformset_factory 对象.因为一个电话可以有很多条线路,所以phone_formset 中的每个电话表单都可以有一个线路表单集对象.我目前正在做这样的事情

My page need to have all these objects editable and new instances created runtime for Phone_Clients and Line all in the same page. Currently I am creating a simple End_User model form and modelformset_factory objects for Phone_Client and Lines. Since a phone can have many lines, each phone form in the phone_formset can have a line formset object. I am currently doing something like this

end_user = End_User.objects.get(pk=user_id)
user_form = End_UserForm(instance=end_user)

Phone_ClientFormSet = modelformset_factory(Phone_Client,form=PartialPhone_ClientForm,  extra=0, can_delete=True)

phone_clients_formset = Phone_ClientFormSet(queryset=end_user.phone_client_set.all(), prefix='phone_client')

all_lines = modelformset_factory(Line, form=PartialLineForm, extra=0, can_delete=True)

phone_clients = end_user.phone_client_set.all()

client_lines_formsets = {}
for phone in phone_clients:
    client_lines_formsets[phone.id] = all_lines(queryset=phone.line_set.all(), prefix='phone_client_'+str(phone.id))

我正在使用此列表使用表单集在模板中显示属于 phone_client 的行.

I am using this list to display lines belonging to a phone_client in the template using formsets.

我有以下问题,关于模型

I have the following question, on the models

  1. 我可以使用 inline_formset 工厂来处理包含直通类的多对多关系吗?如果是这样,我如何通过关系为 Phone_Client、Line 和 Phone_Line 执行此操作?

  1. Can I use inline_formset factory to handle many to many relation containing a through class? if so how do I do that for the Phone_Client, Line and Phone_Line through relation?

我需要显示给定电话、线路组合的 line_index,我该如何在模板中执行此操作?我看过
如何访问多对多通过"的属性?来自 Django 模板的表格?我不想只显示,而是将值绑定到电话、线路组合(如果可能)在线路或电话表单集中,这样如果用户更改索引,我可以在发布表单集数据时将其保存在数据库中.

I need to display the line_index for a given phone, line combination, how do I do that in template? I have looked at
How do I access the properties of a many-to-many "through" table from a django template? I do not want to only display, but bind the value to the phone, line combination if possible in the line or phone formset, such that if user changes the index, I can save that in the data base while posting the formset data.

我是 Django 的新手,所以非常感谢您的帮助.谢谢!!

I am new to django so any help is really appreciated. Thanks!!

推荐答案

您可能知道,您无法使用内联表单集编辑多对多关系.但是,您可以编辑直通模型.因此,对于您的内联表单集,您只需要将模型设置为直通模型,例如:

As you're probably aware, you can't edit many-to-many relationships with inline formsets. You can, however, edit the through model. So for your inline formset, you just need to set the model to the through model, e.g.:

inlineformset_factory(Phone_Client, Line.phone_client.through)

line_index 实际上是内联表单上的一个可见字段,因此您实际上无需执行任何操作.如果有人更改了索引,它会在保存内联表单时保存,就像其他字段一样.

line_index will actually be a visible field on the inline form, so you really don't have to do anything. If someone changes the index, it'll be saved when the inline forms are saved, just like the rest of the fields.

这篇关于访问多对多“通过"Formsets 中的关系字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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