Modify

Ticket #616 (assigned defect)

Opened 4 years ago

Last modified 3 years ago

No support for custom M2M subclasses that change the schema of auto generated through table.

Reported by: gregor@… Owned by: andrew
Priority: major Milestone: 1.0
Component: migrations Version: mercurial
Keywords: m2m, actions Cc:

Description

I'm trying to integrate south support into django-sortedm2m. It's basically a ManyToManyField subclass that overwrites the logic of creating the auto generated through model to add a custom field that is used to change the sort order. However its necessary to trick the M2M field to set SortedM2MThroughModel._meta.auto_create to True. (See http://github.com/gregmuellegger/django-sortedm2m/blob/master/sortedm2m/fields.py for the actual implementation.)

But this breaks south. It handles the SortedManyToManyField like any other ManyToManyField and skips the column the field has added.

I browsed around in the south code and spotted that the fields used for the intermediary table of a M2M field are hard coded. What's about using introspection to determine the fields used on the M2M? We could spit out then the fields that are on the through model dynamically.

I've forked south on bitbucket and pushed some changes to the repository that adds the behaviour I need. I've tested with django 1.1.0 and 1.2.3 which works fine with normal M2M fields and also with the custom one in django-sortedm2m.

I will also add a patch from hg outgoing -p https://gregor_muellegger@bitbucket.org/andrewgodwin/south

A side note: I haven't found any testcases in south which tests the handling of many to many fields. I would like to provide some tests to better demonstrate what I want to achieve. However I had no starting point.

Attachments

m2m_tables.diff (6.3 KB) - added by Gregor Müllegger <gregor@…> 4 years ago.
Introspecting M2M fields to determine the fields on autogenerated through models. Diff between 895f4d0ce7f0 (souths current tip) and d9de9d3964cb (Gregor's changes).

Change History

Changed 4 years ago by Gregor Müllegger <gregor@…>

Introspecting M2M fields to determine the fields on autogenerated through models. Diff between 895f4d0ce7f0 (souths current tip) and d9de9d3964cb (Gregor's changes).

comment:1 Changed 4 years ago by andrew

  • Status changed from new to infoneeded
  • Milestone set to 1.0

To be honest, it was hardcoded since I didn't know of anything that fiddled with M2M relationships that didn't involve a through model.

The patch looks reasonable, but the amount of special-casing in there _just_ to support this one field type worries me slightly - do you not think it would be better to provide a way to get South to just use its normal model creation logic on the implicit through model the M2M must create? That seems more flexible and future-proof.

comment:2 Changed 4 years ago by Gregor Müllegger <gregor@…>

  • Status changed from infoneeded to assigned

Yes, this might be more future-proof. You're right. But of course will need a bit more digging in the code and maybe also some refactoring.

I'll try my luck in the next days.
Thanks for your response :-)

View

Add a comment

Modify Ticket

Action
as assigned
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.