ManyToManyField with multiple model options

625 views
Skip to first unread message

Luke Seelenbinder

unread,
Nov 15, 2008, 9:57:13 AM11/15/08
to Django users
Basically, I want to allow a ManyToManyField to use any of a Model's
children, like this:

class Word(models.Model):
word = models.CharField(...)

class Meta:
abstract = True

class Noun(Word):
gender = models.CharField(...)
class Verb(Word):
irregular = models.BooleanField(...)

#Here is where I need the help
def WordStats(models.Model):
# I get an error saying
word = models.ManyToManyField(Word)
stats = ...

Is there anyway to accomplish this? Without writing separate models
for each word.

Thanks,
Luke S.

Malcolm Tredinnick

unread,
Nov 15, 2008, 8:29:11 PM11/15/08
to django...@googlegroups.com

On Sat, 2008-11-15 at 06:57 -0800, Luke Seelenbinder wrote:
> Basically, I want to allow a ManyToManyField to use any of a Model's
> children, like this:
>
> class Word(models.Model):
> word = models.CharField(...)
>
> class Meta:
> abstract = True
>
> class Noun(Word):
> gender = models.CharField(...)
> class Verb(Word):
> irregular = models.BooleanField(...)
>
> #Here is where I need the help
> def WordStats(models.Model):

This isn't valid Python. It looks like a cross between defining a
function and defining a class.

> word = models.ManyToManyField(Word)

This isn't valid. Model field belong in models, not functions. I suspect
you've just made a typo and typed "def" above when you meant "class". If
you've actually mentioned what the error you were seeing was (or read it
carefully), I suspect that Python would have told you basically that.

> stats = ...
>
> Is there anyway to accomplish this? Without writing separate models
> for each word.

A many-to-many field to the Word model is an appropriate approach.

Regards,
Malcolm


Luke Seelenbinder

unread,
Nov 15, 2008, 10:01:29 PM11/15/08
to Django users
That was a typo. The error I'm getting is
"ForeignKey cannot define a relation with abstract class Word"
It is a ManyToMany through table, that's the reason it is ForeignKey

Luke

On Nov 15, 8:29 pm, Malcolm Tredinnick <malc...@pointy-stick.com>
wrote:

Malcolm Tredinnick

unread,
Nov 15, 2008, 10:13:51 PM11/15/08
to django...@googlegroups.com

On Sat, 2008-11-15 at 19:01 -0800, Luke Seelenbinder wrote:
> That was a typo. The error I'm getting is
> "ForeignKey cannot define a relation with abstract class Word"
> It is a ManyToMany through table, that's the reason it is ForeignKey

One of the many problems with top-posting is that I can't tell what
you're replying to. So I need to guess. I guess that "that was a typo"
is where you used "def" instead of "class"..

> > On Sat, 2008-11-15 at 06:57 -0800, Luke Seelenbinder wrote:
> > > Basically, I want to allow a ManyToManyField to use any of a Model's
> > > children, like this:
> >
> > > class Word(models.Model):
> > > word = models.CharField(...)
> >
> > > class Meta:
> > > abstract = True

You can't do what you're trying to do in that fashion (using
ManyToManyField to an abstract base class). Many-to-many and foreign-key
relations are relations between one table to another table, not to many
other tables. They model the database's idea of relations. That's why
they aren't allowed to be declared for abstract models (which aren't
really models at all).

You could, however, model this using an intermediate table with generic
relations, which allows links to multiple types of remote models through
a content-type field. You won't be able to use anything like "through",
but that's really just a kind of syntactic sugar anyway. You can write
the same sorts of queries referring to the intermediate model
explicitly.

Regards,
Malcolm

Eric Veiras Galisson

unread,
Nov 16, 2008, 12:31:53 PM11/16/08
to django...@googlegroups.com
Hi,

On Sun, Nov 16, 2008 at 4:13 AM, Malcolm Tredinnick
<mal...@pointy-stick.com> wrote:

>
> You can't do what you're trying to do in that fashion (using
> ManyToManyField to an abstract base class). Many-to-many and foreign-key
> relations are relations between one table to another table, not to many
> other tables. They model the database's idea of relations. That's why
> they aren't allowed to be declared for abstract models (which aren't
> really models at all).
>
> You could, however, model this using an intermediate table with generic
> relations, which allows links to multiple types of remote models through
> a content-type field. You won't be able to use anything like "through",
> but that's really just a kind of syntactic sugar anyway. You can write
> the same sorts of queries referring to the intermediate model
> explicitly.

As I'm concerned by the same problem (refering by a ForeignKey to an
abstract class) and don't want to make my abstract class concrete, I'm
interested in the solution you talk about.
Can you explain me more what do you think about, I don't really
understand how I can do that?

Thanks in advance.

--
Eric Veiras Galisson
http://www.veiras.info

Reply all
Reply to author
Forward
0 new messages