Hi,

I have my models defined like this.

class Image(models.Model):
    description = models.CharField(max_length=500)
    .........

class ImageStatic(models.Model):
    """ Image metadata not under change control
    """
    ....................
    def latest(self):
        return self.image_set.order_by('image_revision')[0]

    def first(self):
        return self.image_set.order_by('-image_revision')[0]


class Revision(models.Model):
    """A report of an older version of some Article."""

    revision_id = models.PositiveIntegerField(_(u"Revision Number"))
    .................................



class ImageRevision(Revision):
    """A report of an older version of some Image."""
    image_static = models.ForeignKey('ImageStatic')


I have used multi table inheritance for the ImageRevision model. When
I call image static objects's latest or first method getting a sql
error.

project = get_object_or_404(Project, instance='gw')
image_static = get_object_or_404(ALL_IMAGES, pk=54, project=project)
rev = image_static.imagerevision_set.count()
revision_filter = {'revision_id':rev,'image_static':image_static}
latest_revision = get_object_or_404(ImageRevision,**revision_filter)
obj = Image.objects.get(image_static=image_static,
image_revision=latest_revision)

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Python25\Lib\site-packages\django\db\models\query.py", line
238, in _
_getitem__
    return list(qs)[0]
  File "C:\Python25\Lib\site-packages\django\db\models\query.py", line
162, in _
_len__
    self._result_cache.extend(list(self._iter))
  File "C:\Python25\Lib\site-packages\django\db\models\query.py", line
275, in i
terator
    for row in self.query.results_iter():
  File "C:\Python25\Lib\site-packages\django\db\models\sql\query.py",
line 206,
in results_iter
    for rows in self.execute_sql(MULTI):
  File "C:\Python25\Lib\site-packages\django\db\models\sql\query.py",
line 1724,
 in execute_sql
    sql, params = self.as_sql()
  File "C:\Python25\Lib\site-packages\django\db\models\sql\query.py",
line 261,
in as_sql
    ordering = self.get_ordering()
  File "C:\Python25\Lib\site-packages\django\db\models\sql\query.py",
line 656,
in get_ordering
    self.model._meta, default_order=asc):
  File "C:\Python25\Lib\site-packages\django\db\models\sql\query.py",
line 684,
in find_ordering_name
    opts, alias, False)
  File "C:\Python25\Lib\site-packages\django\db\models\sql\query.py",
line 1321,
 in setup_joins
    "Choices are: %s" % (name, ", ".join(names)))
FieldError: Cannot resolve keyword 'revision' into field. Choices are:
article,
description, image, image_revision, image_static
>>> rev = image_static.image_set.order_by('imagerevision')[0]
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Python25\Lib\site-packages\django\db\models\query.py", line
238, in _
_getitem__
    return list(qs)[0]
  File "C:\Python25\Lib\site-packages\django\db\models\query.py", line
162, in _
_len__
    self._result_cache.extend(list(self._iter))
  File "C:\Python25\Lib\site-packages\django\db\models\query.py", line
275, in i
terator
    for row in self.query.results_iter():
  File "C:\Python25\Lib\site-packages\django\db\models\sql\query.py",
line 206,
in results_iter
    for rows in self.execute_sql(MULTI):
  File "C:\Python25\Lib\site-packages\django\db\models\sql\query.py",
line 1724,
 in execute_sql
    sql, params = self.as_sql()
  File "C:\Python25\Lib\site-packages\django\db\models\sql\query.py",
line 261,
in as_sql
    ordering = self.get_ordering()
  File "C:\Python25\Lib\site-packages\django\db\models\sql\query.py",
line 656,
in get_ordering
    self.model._meta, default_order=asc):
  File "C:\Python25\Lib\site-packages\django\db\models\sql\query.py",
line 684,
in find_ordering_name
    opts, alias, False)
  File "C:\Python25\Lib\site-packages\django\db\models\sql\query.py",
line 1321,
 in setup_joins
    "Choices are: %s" % (name, ", ".join(names)))
FieldError: Cannot resolve keyword 'imagerevision' into field. Choices
are: arti
cle, description, image, image_revision, image_static
>>> rev = image_static.image_set.order_by('image_revision')[0]
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Python25\Lib\site-packages\django\db\models\query.py", line
238, in _
_getitem__
    return list(qs)[0]
  File "C:\Python25\Lib\site-packages\django\db\models\query.py", line
162, in _
_len__
    self._result_cache.extend(list(self._iter))
  File "C:\Python25\Lib\site-packages\django\db\models\query.py", line
275, in i
terator
    for row in self.query.results_iter():
  File "C:\Python25\Lib\site-packages\django\db\models\sql\query.py",
line 206,
in results_iter
    for rows in self.execute_sql(MULTI):
  File "C:\Python25\Lib\site-packages\django\db\models\sql\query.py",
line 1734,
 in execute_sql
    cursor.execute(sql, params)
  File "C:\Python25\Lib\site-packages\django\db\backends\util.py",
line 19, in e
xecute
    return self.cursor.execute(sql, params)
  File "C:\Python25\Lib\site-packages\django\db\backends\mysql
\base.py", line 83
, in execute
    return self.cursor.execute(query, args)
  File "C:\Python25\lib\site-packages\MySQLdb\cursors.py", line 166,
in execute
    self.errorhandler(self, exc, value)
  File "C:\Python25\lib\site-packages\MySQLdb\connections.py", line
35, in defau
lterrorhandler
    raise errorclass, errorvalue
OperationalError: (1054, "Unknown column
'cms_imagerevision.revision_ptr_id' in
'on clause'")

What I can see from the sql query is that it is joinning parent table
and in the ON clause using derrived class's name? I know to change the
sql so that int he join statement use child table instead of parent
table that will solve it. But with django usage not sure how to solve
it.

Here is the sql it generates:

SELECT `cms_image`.`description`, `cms_image`.`image`, `c
ms_image`.`image_revision_id`, `cms_image`.`image_static_id` FROM
`cms_image` IN
NER JOIN `cms_revision` ON (`cms_imagerevision`.`revision_ptr_id` =
`cms_revisio
n`.`id`) WHERE (`cms_image`.`image_static_id` = 54  AND
`cms_image`.`image_revisi
on_id` = 33 ) ORDER BY `cms_image`.`image_revision_id` DESC'


What I understood from this is I always have to use parent instead of
child in other models. But I really think you should be able to use
child tables that is what inheritance is for.

Could someone let me know how to solve this?

Thanks in advance.








--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-developers@googlegroups.com
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to