Access to many fields "through" in "Forms"

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

Thus, basically one end user can have many phones, a phone can have many lines connected via Phone_line.

My page should have all these objects for editing, and new instances created the runtime for Phone_Clients and Line all on one page. I am currently 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 phone_formset can have a formetet object. I'm 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 use this list to display strings belonging to phone_client in a template using forms.

I have the following question on models

  • Is it possible to use the inline_formset factory to handle many, many relationships containing an end-to-end class? if so, how to do this for Phone_Client, Line and Phone_Line through a relationship?

  • I need to display the line_index line for this phone, a combination of lines, how to do this in the template? I looked at How do I access set-to-many properties through a table from a django template? I don’t want to show only, but to bind the value to the phone, a combination of strings, if possible, in the string or phone, so if the user changes the index, I can save it in the database when sending form set data.

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

+7
source share
1 answer

As you probably know, you cannot edit many-to-many relationships with built-in formsets. However, you can edit the pass-through model. So, for your inline set of forms, you just need to set the model to an end-to-end model, for example:

 inlineformset_factory(Phone_Client, Line.phone_client.through) 

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

+14
source