Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save a ManyToMany field with a through relationship

I have the following models with a ManyToMany and through relationship:

class Meeting(models.Model):
    site = models.ForeignKey(Site)
    meeting_title = models.CharField(default='', max_length=128, blank=True, null=True)
    meeting_visitors = models.ManyToManyField(Visitor, through="MeetingArrival", blank=False, null=False) 

class Visitor(models.Model):
    visitor_company = models.ForeignKey(Company)
    visitor_name = models.CharField(default='', max_length=128, blank=False, null=False)

class MeetingArrival(models.Model):
    visitor = models.ForeignKey(Visitor)
    meeting = models.ForeignKey(Meeting)
    arrival_status = models.BooleanField(default=False)

I have a form to create a meeting:

class AddMeetingForm(forms.ModelForm):

    class Meta:
        model = Meeting
        exclude = ['site',]

And a simple view to save the form:

def add_meeting(request): 
    add_meeting_form = AddMeetingForm(request.POST or None)
    site = Site.objects.get(user=request.user.id)

    if request.method == "POST":
        if add_meeting_form.is_valid():

            obj = add_meeting_form.save(commit=False)
            obj.site = site
            obj.save()

This saves the form, but not the meeting_visitors field, even though this field renders perfectly in the view. How do I save this relationship?

EDIT

If I add add_meeting_form.save_m2m() to the view, I get Cannot set values on a ManyToManyField which specifies an intermediary model. Use meetings.MeetingArrival's Manager instead.. How would I do this?

like image 521
alias51 Avatar asked Aug 06 '15 17:08

alias51


4 Answers

You will have to explicitly save the MeetingArrival object in your view to save an intermediate model in case of a ManyToManyField with through argument.

For Django versions 2.1 and below, in case of ManyToManyField with an intermediary model, you can’t use add, create, or assignment which are available with normal many-to-many fields.

As per the Django 1.8 docs:

Unlike normal many-to-many fields, you can’t use add, create, or assignment to create relationships.

The only way to create this type of relationship is to create instances of the intermediate model.

So, you will have to explicitly create a MeetingArrival object in your view.

You can do it by:

def add_meeting(request): 
    add_meeting_form = AddMeetingForm(request.POST or None)
    site = Site.objects.get(user=request.user.id)

    if request.method == "POST":
        if add_meeting_form.is_valid():
            obj = add_meeting_form.save(commit=False)
            obj.site = site
            obj.save()

            # create an instance of 'MeetingArrival' object
            meeting_arrival_obj = MeetingArrival(meeting=obj, visitor=<your_visitor_object_here>, arrival_status=True)
            meeting_arrival_obj.save() # save the object in the db
like image 175
Rahul Gupta Avatar answered Oct 20 '22 21:10

Rahul Gupta


for django 1.x, it is as what Rahul said that you can't use add, create, etc

for django 2.x, you actually can per the document here django 2.x

You can also use add(), create(), or set() to create relationships, as long as you specify through_defaults for any required fields:

beatles.members.add(john, through_defaults={'date_joined': date(1960, 8, 1)})
beatles.members.create(name="George Harrison", through_defaults={'date_joined': date(1960, 8, 1)})
beatles.members.set([john, paul, ringo, george], through_defaults={'date_joined': date(1960, 8, 1)})
like image 35
miaoz2001 Avatar answered Oct 20 '22 21:10

miaoz2001


When you use a through table, you need to save there manually.

MeetingArrival.objects.create( ... )
like image 36
Gocht Avatar answered Oct 20 '22 20:10

Gocht


Don't put the logic where you handle the ManyToManyField in your view, put it in your form instead, so you won't have to repeat yourself if you use the form in multiple places.

To this end, you need to override the save method of the ModelForm.

See my more thorough answer to another question: https://stackoverflow.com/a/40822731/2863603

like image 38
mehmet Avatar answered Oct 20 '22 19:10

mehmet