AttributeError: 'GenericImport' object has no attribute 'account_import_set'
I have a model AccountImport
that very clearly has a foreign key to GenericImport
:
from django.db import models from mcif.models.generic_import import GenericImport
class AccountImport(models.Model):
id = models.BigIntegerField(primary_key=True)
generic_import = models.ForeignKey(GenericImport)
is_entirely_international = models.IntegerField()
is_queued = models.IntegerField()
created_at = models.DateTimeField()
updated_at = models.DateTimeField()
class Meta:
db_table = u'account_import'
app_name = 'mcif'
And here's GenericImport
:
from django.db import models
fr开发者_C百科om mcif.models.import_profile import ImportProfile
from mcif.models.import_file import ImportFile
from mcif.models.import_bundle import ImportBundle
from mcif.models.customer import Customer
from mcif.models.csv_row import CSVRow
import csv, cStringIO
class GenericImport(models.Model):
class Meta:
db_table = u'generic_import'
app_name = 'mcif'
id = models.BigIntegerField(primary_key=True)
import_profile = models.ForeignKey(ImportProfile)
import_file = models.ForeignKey(ImportFile)
notes = models.TextField()
start_time = models.DateTimeField()
end_time = models.DateTimeField()
active = models.IntegerField()
created_at = models.DateTimeField()
updated_at = models.DateTimeField()
unsavable_rows = models.TextField()
import_bundle = models.ForeignKey(ImportBundle)
is_queued = models.IntegerField()
@classmethod
def last(cls):
all = GenericImport.objects.all()
return all[len(all) - 1]
def process(self):
for line in self.import_file.file.split("\n")[:30]:
f = cStringIO.StringIO(line)
row = CSVRow()
row.array = next(csv.reader(f))
row.generic_import = self
row.process()
f.close()
But watch what happens when I do this:
>>> from mcif.models.generic_import import GenericImport
>>> GenericImport.objects.all()[0].account_import_set.all()
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'GenericImport' object has no attribute 'account_import_set'
Isn't that supposed to work? I don't understand what I could have done wrong.
It should be your model name in lowercase unless overriden by related_name
.
http://docs.djangoproject.com/en/dev/topics/db/queries/#following-relationships-backward
GenericImport.objects.latest('id').accountimport_set.all()
精彩评论