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

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

问题描述

我的模型:

  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相关联。



我的页面需要全部这些对象可编辑,新的实例为Phone_Clients创建了运行时,Line都在同一页面中。目前我正在为Phone_Client和Lines创建一个简单的End_User模型表单和modelformset_factory对象。由于手机可以有多行,所以phone_formset中的每个手机表单都可以有一个线形表单对象。我正在做这样的事情

  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 = {}
在phone_clients中的电话:
client_lines_formsets [phone.id] = all_lines(queryset = phone.line_set.all(),prefix ='phone_client_ '+ str(phone.id))

我正在使用此列表显示属于phone_client的行在模板中使用formets。



我有以下问题,在模型上


    <我可以使用

    inline_formset工厂处理许多关系包含一个通过类?如果是这样,通过关系为Phone_Client,Line和Phone_Line这样做?


  1. 我需要显示给定手机的line_index,线组合,如何我在模板中这样做吗?我看过

    如何访问多对多通过的属性。从django模板的表?$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $我不想只显示,但绑定到手机,行组合如果可能在行或电话表单集,这样,如果用户更改索引,可以在发布表单数据时将数据保存在数据库中。


我是django的新手,所以任何帮助真的赞赏。
谢谢!!

如您所知,您无法编辑多对多关系与内联形式。但是,您可以编辑直通模型。因此,对于您的内联表单,您只需要将模型设置为直通模型,例如:

  inlineformset_factory(Phone_Client,Line。 phone_client.through)

line_index 实际上是一个可见的字段在内联形式,所以你真的不必做任何事情。如果有人更改了索引,当保存内联表单时,就像其余的字段一样保存。


My Models:

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()

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

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))

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. 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?

  2. 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.

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 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.

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

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