Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Ping for ticket #6095 (m2m intermediary model)

4 views
Skip to first unread message

flo...@gmail.com

unread,
Dec 21, 2007, 4:48:51 PM12/21/07
to Django developers
Hello All,

I've been working on adding m2m intermediary model functionality for a
while now, and the patch is to the point where I can't seem to find
any more edge cases to address. At this point it needs people to try
it out and/or review the code. There are up-to-date tests and
documentation to go along with the patch, by the way.

The files to look at are 6095-alpha-05.diff, and docs.diff.

I know that everyone's busy, but I just wanted to bring this up on-
list to raise awareness.

Thanks,
Eric Florenzano

Jacob Kaplan-Moss

unread,
Dec 21, 2007, 5:00:51 PM12/21/07
to django-d...@googlegroups.com
Hey Eric --

Yeah, I've been keeping an eye on your work; looks pretty nice! I'm on
my way to my parents' house for the holidays, so the chance of me
actually doing anything with your patch soon is pretty low, but I'll
do my best to get to it as soon as I get back.

Thanks!

Jacob

Russell Keith-Magee

unread,
Dec 21, 2007, 10:49:06 PM12/21/07
to django-d...@googlegroups.com

Hi Eric,

I've been keeping an eye on this patch, but I haven't had a chance for
a week or so, what with the general pre-Christmas rush. I'll see if I
can find some time to take the patch for a spin, but it could be about
a week or so before that happens.

Yours,
Russ Magee %-)

flo...@gmail.com

unread,
Dec 22, 2007, 1:59:03 AM12/22/07
to Django developers
Hello,

Thanks for the responses guys! Have fun with your holiday
celebrations.

-Eric Florenzano

On Dec 21, 9:49 pm, "Russell Keith-Magee" <freakboy3...@gmail.com>
wrote:

David Cramer

unread,
Dec 22, 2007, 2:14:39 PM12/22/07
to Django developers
This is something we could really use.

I'm assuming to access those attributes they're just appended on the
new objects created?

So I could have:

MyModel():
relationships = models.ManyToManyField(ContentType,
through="Relationship")

Relationship():
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerFIeld()
some_cool_field = models.DateTimeField()

And this would work? I may be reading this wrong..

Then, if this does work, and I did MyModel.relationships.all(), i'd be
able to say instance.some_cool_field?

flo...@gmail.com

unread,
Dec 23, 2007, 2:44:08 AM12/23/07
to Django developers
I not sure, but I think that what you're talking about is closer to
model inheritance. This patch is more about supplying an explicit
model for many-to-many relationships to use as its intermediary
table. It's more like this:

class Song(models.Model):
name = models.CharField(max_length=128)

class Album(models.Model):
name = models.CharField(max_length=128)
songs = models.ManyToManyField(Song, through='AlbumSongMap')

class AlbumSongMap(models.Model):
song = models.ForeignKey(Song)
album = models.ForeignKey(Album)
is_remix = models.BooleanField(default=False)

Now you could do album_instance.songs.all() and get all of the related
song objects, song_instance.album_set.all() and get all of the related
album objects, or AlbumSongMap.objects.filter(song=song_instance,
album=album_instance) to get the relationship data. Then you can set
properties on the relationship, like is_remix = True if it's a remix
of the original song.

Ok so my example isn't that good, but you can see how it can be useful
for setting properties on _relationships_ between models, instead of
setting them on models themselves. Plus, explicit is better than
implicit :)

Thanks,
Eric Florenzano

David Cramer

unread,
Dec 23, 2007, 8:20:26 PM12/23/07
to Django developers
Ya that's what I'm getting at.

You can filter, as per your example, but can you then access those
arguments? My example wasn't too good, as it's more of a generic
relation use case of what this is fixing.

flo...@gmail.com

unread,
Dec 23, 2007, 10:51:07 PM12/23/07
to Django developers
I'm not 100% sure what the question is. Are you asking if the
additional fields on AlbumSongMap are accessible directly from the
Album or Song models? If so, then no that's not the case with the
current patch. It is however possible to use the QuerySet API to
query for Album or Song objects given a certain property on
AlbumSongMap. To actually set those fields to be something other than
null or the default, right now you have to query for, change, and save
it through AlbumSongMap's manager.

I do think that there's a bit room for improvement here in terms of
syntactical sugar. The problem is that with
album_instance.songs.add(...), that 'add' function takes *args and
adds each arg to the song set. We could do something like
album_instance.songs.add_extra(song, extra={'is_remix' : True}) but
I'm not extremely happy with that syntax or with the fact that we'd be
duplicating functionality. A way to respect DRY might be to do
something like album_instance.songs.add({'instance' : song, 'extra' :
{'is_remix' : True}}, ...) and have 'add' check whether each arg is an
instance or a dictionary, but that syntax is horrible IMHO. If
someone could propose a better syntax, I'd be happy to implement it,
but right now I see no elegant solution.

Hopefully I've answered your question! If not, just keep asking and
eventually I'll catch on.

Thanks,
Eric Florenzano
Reply all
Reply to author
Forward
0 new messages