I have a postgres database (8.1.11) with > 200 tables that has regular
minor changes(most of them adding fields or tables). So I want to use
model inheritance to separate logic form model definition. Inspectdb
creates the models which I then edit to make abstract base classes.
I'm running Django 7673. Note: I cannot alter the schema without
breaking other software that relies on it.
The problem is in the reverse relations part.
When using the models below I can do the following:
f = mgdb_Features.objects.get(pk=10) #gives me a distinct feature fom
the database, OK
fl= f.mgdb_featureloc_set.all() #should give me all feature locations
for 'f'.
Looking at the querie that Django produces, it does exactly this, but
it joins to the last ForeignKey definition 'srcfeature' in
mgdb_Featureloc.
However I want to have it joined on 'feature' instead of 'srcfeature'.
According to the documentation i should set a unique 'related_name'
atribute to force the join on the feature field instead of the
srcfeature field of the featureloc table.
When I change the definition of the feature foreignKey to:
feature = models.ForeignKey('mgdb_Feature', related_name="floc_set")
I get the folloing error:
Traceback (most recent call last):
File "<console>", line 1, in ?
AttributeError: 'QuerySet' object has no attribute 'featureloc_id'
I guess this happens because it tries to look at the abstract class
instead of the child class, despite the fact that I explicitly relate
to the child class in the ForeignKey definition.
Question: How can I use the related_name setting to get the desired
functionality for the child classes reverse lookups?
To cut things short, here is an example of the models stripped from
irrelevant things:
#abstract base classes
class Feature(models.Model):
feature_id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=255)
type = models.ForeignKey('mgdb_Cvterm')
class Meta:
abstract=True
class Featureloc(models.Model):
featureloc_id = models.IntegerField(primary_key=True)
feature = models.ForeignKey('mgdb_Feature') # i'm linking to the
non-abstract child class here
srcfeature = models.ForeignKey('mgdb_Feature') # i'm linking to
the non-abstract child class here
fmin = models.IntegerField()
fmax = models.IntegerField()
class Meta:
abstract=True
# child classes
class mgdb_Feature(Feature):
class Meta(Feature.Meta):
db_table = u'feature'
def __unicode__(self):
return self.name
class mgdb_Featureloc(Featureloc):
class Meta(Featureloc.Meta):
db_table = u'featureloc'
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---