Note that there are some explanatory texts on larger screens.

plurals
  1. POSave M2M "Through" Inlines in Django Admin
    primarykey
    data
    text
    <p>Apparently Django's ModelAdmin/ModelForm doesn't allow you to use save_m2m() if there's an intermediate through table for a ManyToManyField.</p> <p>models.py:</p> <pre><code>from django.db import models def make_uuid(): import uuid return uuid.uuid4().hex class MyModel(models.Model): id = models.CharField(default=make_uuid, max_length=32, primary_key=True) title = models.CharField(max_length=32) many = models.ManyToManyField("RelatedModel", through="RelatedToMyModel") def save(self, *args, **kwargs): if not self.id: self.id = make_uuid() super(GuidPk, self).save(*args, **kwargs) class RelatedModel(models.Model): field = models.CharField(max_length=32) class RelatedToMyModel(models.Model): my_model = models.ForeignKey(MyModel) related_model = models.ForeignKey(RelatedModel) additional_field = models.CharField(max_length=32) </code></pre> <p>admin.py:</p> <pre><code>from django import forms from django.contrib import admin from .models import MyModel class RelatedToMyModelInline(admin.TabularInline): model = MyModel.many.through class MyModelAdminForm(forms.ModelForm): class Meta: model = MyModel class MyModelAdmin(admin.ModelAdmin): form = MyModelAdminForm inlines = (RelatedToMyModelInline, ) admin.site.register(MyModel, MyModelAdmin) </code></pre> <p>If I save MyModel first and then add a new related through model via the inline it works fine, but if I try to set the inline while also adding data for a new MyModel, I get the Django Admin error "Please correct the error below." with nothing highlighted below.</p> <p>How can I have it save MyModel and then save the inline intermediary models after? Clearly Django can save the through model once it has saved MyModel - so I'm just looking for a hook into that. I tried overriding the form's save() method by calling save_m2m() after calling instance.save(), but apparently that doesn't work for M2Ms with a through table.</p> <p>I'm using Django 1.2, but this is still an issue in 1.3.</p> <p><strong>UPDATE:</strong> Well, I made a test app like above to isolate the problem, and it appears that it works as expected, correctly saving the M2M intermediary object after saving the MyModel object... as long as I let Django automatically create the MyModel.id field when running <code>python manage.py syncdb</code> - once I added the GUID id field, it no longer works.</p> <p>This smells more and more like a Django bug.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload