On 19/05/10 10:42, Daniel Roseman wrote:
How remove PK from table "libs_content_has_people" and don't use it?
You can't. Why would you want to?
Well, the natural primary key for a M2M table is quite typically
composite and django's ORM just doesn't support composite primary keys
right now. IF you're trying to wrap an existing DB that uses composite
primary keys with the django ORM, it's kind of a pain. Of course, you
don't _have_ to use the django ORM if it's not fit for that purpose -
SQLAlchemy supports composite primary keys fine IIRC, but is all a
little more complicated than the Django ORM.
One "trick" that can help right now in some circumstances is to just lie
and set the declared django model primary key field to just one of the
columns in the true composite primary key. Then _reading_ data via the
django ORM (at least 1.1.x, haven't got to trying the trick on 1.2 yet)
will actually "happen to work" ...Just don't try to save!
I for one do believe composite keys could be added to the django orm
without really hurting its user interface simplicity. Though not without
complicating its implementation of course. Say have a CompositeField
model field that takes a list of underlying fields and translates
to/from some dict-like CompositeValue python object wrapping the
relevant underlying fields of the instance. ...and then "just" rework
the ORM layer be able to handle them and constraints on them, including
use as a primary key.
Wouldn't it be nice to be able to say
class OhAndAPonyToo(models.Model):
class Meta:
db_table='existing_table_that_i_cannot_change'
people = models.ForeignKey(People)
content = models.ForeignKey(Content)
id = models.CompositeField(('people', 'content'), primary_key=True)
pony = models.CharField(max_length=10)
I expect CompositeForeignKey and perhaps even CompositeManyToMany and
CompositeManyToCompositeMany fields could also be defined
(somewhat akin to GenericForeignKey implementation-wise)
--
You received this message because you are subscribed to the Google Groups "Django
users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.