Apparently Django's ModelAdmin/ModelForm doesn't allow you to use
save_m2m() if there's an intermediate through table for a
ManyToManyField.
If I have a model such as:
class MyModel(models.Model):
created = models.DateTimeField()
many = models.ManyToManyField("RelatedModel",
through="RelatedToMyModel")
def save(self, *args, **kwargs):
self.created = datetime.datetime.now()
super(MyModel, 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)
And you use a simple Django ModelAdmin:
class = RelatedToMyModelInline(admin.TabularInline):
model = MyModel.many.through
class MyModelAdminForm(forms.ModelForm):
class Meta:
model = MyModel
class MyModelAdmin(admin.ModelAdmin):
form = MyModelAdminForm
inlines = (RelatedToMyModelInline, )
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.
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.
I'm using Django 1.2, but apparently this is still an issue in 1.3.
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.