On Mon, Mar 21, 2011 at 12:33:01AM -0700, Christophe Pettus wrote: > I'd like to make one more pitch for a slightly different > implementation here. My concern with CompositeField isn't based on > the fact that it doesn't map one-to-one with a field in the table; > it's that it doesn't have any of the semantics that are associated > with a field. In particular, it can't be: > > - Assigned to. > - Iterated over. > - Or even have a value. I disagree. The CompositeField would need to have a value to be able to implement a ForeignKey pointing to a modedel with a composite primary key.
The CompositeField itself would be just a proxy to the actual atomic fields. You should be able to assign a tuple (or namedtuple) to it, specifying the actual values for the fields. Similarly, you'll be able to retrieve its value which is a tuple or a namedtuple. This way, the following code would work for composite primary keys the same way it works for simple keys: class CompositeModel(models.Model): a = models.IntegerField() b = models.IntegerField() key = models.CompositeField((a, b), primary_key=True) class ReferencingModel(models.Model): cm = models.ForeignKey(CompositeModel) cminstance = CompositeModel.objects.get(something) newref = ReferencingModel() newref.cm = cminstance.pk > My suggestion is to create an Index type that can be included in a > class just like a field can. The example we've been using would > then look like: > > class Foo(Model): > x = models.FloatField() > y = models.FloatField() > a = models.ForeignKey(A) > b = models.ForeignKey(B) > > coords = models.CompositeIndex((x, y)) > pair = models.CompositeIndex((a, b), primary_key=True) > > We could have FieldIndex (the equivalent of the current > db_index=True), CompositeIndex, and RawIndex, for things like > expression indexes and other things that can be specified just as a > raw SQL string. > > I think this is a much better contract to offer in the API than one > based on field which would have to throw exceptions left and right > for most of the common field operations. I don't see how ForeignKeys would be possible this way. Michal Petrucha
signature.asc
Description: Digital signature