bgrantdev
bgrantdev

Reputation: 1672

Django Related model cannot be resolved

I have two django/python applications one is running on Django 1.8 and Python 3.4 and the other is running on Django 1.8 and Python 2.7. These applications share a database and use a python package that houses several of the models that are shared between the two applications in a few different apps.

The application running on 3.4 works fine but the application running on 2.7 throws the ValueError: Relate Model 'model_reference' cannot be resolved.

In this psuedo example the package is core_app the two models are within seperate apps called foobar and barfoo contained in core_app.

foobar/models.py

class Model_A(models.Model):
   name = TextField()

barfoo/models.py

class Model_B(models.Model):
   model_a = ForeignKey('core_app_foobar.Model_A')

Here is the full stack trace.

    /home/ubuntu/moi/local/lib/python2.7/site-packages/django/db/models/manager.pyc in manager_method(self, *args, **kwargs)
    125         def create_method(name, method):
    126             def manager_method(self, *args, **kwargs):
--> 127                 return getattr(self.get_queryset(), name)(*args, **kwargs)
    128             manager_method.__name__ = method.__name__
    129             manager_method.__doc__ = method.__doc__

/home/ubuntu/moi/local/lib/python2.7/site-packages/django/db/models/query.pyc in get(self, *args, **kwargs)
    326         if self.query.can_filter():
    327             clone = clone.order_by()
--> 328         num = len(clone)
    329         if num == 1:
    330             return clone._result_cache[0]

/home/ubuntu/moi/local/lib/python2.7/site-packages/django/db/models/query.pyc in __len__(self)
    142 
    143     def __len__(self):
--> 144         self._fetch_all()
    145         return len(self._result_cache)
    146 

/home/ubuntu/moi/local/lib/python2.7/site-packages/django/db/models/query.pyc in _fetch_all(self)
    963     def _fetch_all(self):
    964         if self._result_cache is None:
--> 965             self._result_cache = list(self.iterator())
    966         if self._prefetch_related_lookups and not self._prefetch_done:
    967             self._prefetch_related_objects()

/home/ubuntu/moi/local/lib/python2.7/site-packages/django/db/models/query.pyc in iterator(self)
    236         # Execute the query. This will also fill compiler.select, klass_info,
    237         # and annotations.
--> 238         results = compiler.execute_sql()
    239         select, klass_info, annotation_col_map = (compiler.select, compiler.klass_info,
    240                                                   compiler.annotation_col_map)

/home/ubuntu/moi/local/lib/python2.7/site-packages/django/db/models/sql/compiler.pyc in execute_sql(self, result_type)
    827             result_type = NO_RESULTS
    828         try:
--> 829             sql, params = self.as_sql()
    830             if not sql:
    831                 raise EmptyResultSet

/home/ubuntu/moi/local/lib/python2.7/site-packages/django/db/models/sql/compiler.pyc in as_sql(self, with_limits, with_col_aliases, subquery)
    376         refcounts_before = self.query.alias_refcount.copy()
    377         try:
--> 378             extra_select, order_by, group_by = self.pre_sql_setup()
    379             if with_limits and self.query.low_mark == self.query.high_mark:
    380                 return '', ()

/home/ubuntu/moi/local/lib/python2.7/site-packages/django/db/models/sql/compiler.pyc in pre_sql_setup(self)
     46         might not have all the pieces in place at that time.
     47         """
---> 48         self.setup_query()
     49         order_by = self.get_order_by()
     50         extra_select = self.get_extra_select(order_by, self.select)

/home/ubuntu/moi/local/lib/python2.7/site-packages/django/db/models/sql/compiler.pyc in setup_query(self)
     37         if all(self.query.alias_refcount[a] == 0 for a in self.query.tables):
     38             self.query.get_initial_alias()
---> 39         self.select, self.klass_info, self.annotation_col_map = self.get_select()
     40         self.col_count = len(self.select)
     41 

/home/ubuntu/moi/local/lib/python2.7/site-packages/django/db/models/sql/compiler.pyc in get_select(self)
    185         if self.query.default_cols:
    186             select_list = []
--> 187             for c in self.get_default_columns():
    188                 select_list.append(select_idx)
    189                 select.append((c, None))

/home/ubuntu/moi/local/lib/python2.7/site-packages/django/db/models/sql/compiler.pyc in get_default_columns(self, start_alias, opts, from_parent)
    522             alias = self.query.join_parent_model(opts, model, start_alias,
    523                                                  seen_models)
--> 524             column = field.get_col(alias)
    525             result.append(column)
    526         return result

/home/ubuntu/moi/local/lib/python2.7/site-packages/django/db/models/fields/related.pyc in get_col(self, alias, output_field)
   2015 
   2016     def get_col(self, alias, output_field=None):
-> 2017         return super(ForeignKey, self).get_col(alias, output_field or self.related_field)
   2018 
   2019 

/home/ubuntu/moi/local/lib/python2.7/site-packages/django/db/models/fields/related.pyc in related_field(self)
   1895     @property
   1896     def related_field(self):
-> 1897         return self.foreign_related_fields[0]
   1898 
   1899     def get_reverse_path_info(self):

/home/ubuntu/moi/local/lib/python2.7/site-packages/django/db/models/fields/related.pyc in foreign_related_fields(self)
   1629     @property
   1630     def foreign_related_fields(self):
-> 1631         return tuple(rhs_field for lhs_field, rhs_field in self.related_fields)
   1632 
   1633     def get_local_related_value(self, instance):

/home/ubuntu/moi/local/lib/python2.7/site-packages/django/db/models/fields/related.pyc in related_fields(self)
   1616     def related_fields(self):
   1617         if not hasattr(self, '_related_fields'):
-> 1618             self._related_fields = self.resolve_related_fields()
   1619         return self._related_fields
   1620 

/home/ubuntu/moi/local/lib/python2.7/site-packages/django/db/models/fields/related.pyc in resolve_related_fields(self)
   1601             raise ValueError('Foreign Object from and to fields must be the same non-zero length')
   1602         if isinstance(self.rel.to, six.string_types):
-> 1603             raise ValueError('Related model %r cannot be resolved' % self.rel.to)
   1604         related_fields = []
   1605         for index in range(len(self.from_fields)):

ValueError: Related model 'core_app_foobar.Model_A' cannot be resolved

Upvotes: 5

Views: 2895

Answers (2)

Saurabh Goyal
Saurabh Goyal

Reputation: 647

you can do it in one of the following ways-

In barfoo/models.py-

1)

from django.db import models

class Model_B(models.Model):
    model_a = models.ForeignKey('foobar.Model_A')

2)

from django.db import models
from core_app.foobar import models as foobar_models

class Model_B(models.Model):
    model_a = models.ForeignKey(foobar_models.Model_A)

Upvotes: -1

Paulo Pessoa
Paulo Pessoa

Reputation: 2569

Try it:

class Model_B(models.Model):
   model_a = ForeignKey('foobar.Model_A')

don't forget to inherit from django models. (both)

from django.db import models

To refer to models defined in another application, you can explicitly specify a model with the full application label. For example, if the Manufacturer model above is defined in another application called production, you’d need to use:

Doc ForeignKey

Upvotes: -1

Related Questions