2015-09-25 96 views
1

我有兩個django/python應用程序,一個在Django 1.8和Python 3.4上運行,另一個在Django 1.8和Python 2.7上運行。這些應用程序共享一個數據庫,並使用一個python包,其中包含幾個不同應用程序中的兩個應用程序之間共享的幾個模型。Django相關模型無法解析

在3.4上運行的應用程序工作正常,但在2.7上運行的應用程序會引發ValueError:相關模型'model_reference'無法解析。

在這個psuedo示例中,軟件包是core_app,這兩個模型位於core_app中包含的稱爲foobar和barfoo的單獨應用程序中。

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') 

以下是完整的堆棧跟蹤。

/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 
+0

是自關係? –

+1

對不起,這是示例代碼中的拼寫錯誤。 Model_B引用Model_A – bgrantdev

回答

-1

試試:

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

不要忘了從Django模型繼承。 (兩者)

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

+1

我可以知道downvotes的原因嗎? –

+0

模型繼承不是問題,這是簡化示例中的錯誤。如果這是問題,那麼拋出的錯誤將會不同。此外,該模型使用完整的應用程序標籤進行指定,並且正如在運行1.8/3.4的應用程序中所述的那樣工作正常,此問題僅在運行1.8/2.7的應用程序中出現。 – bgrantdev

+0

那麼你的問題措辭錯了,因爲顯然他們看到的第一個問題是你的應用程序的名稱與導入的名稱不同。 –

-1

您可以通過以下ways-

一個做在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) 
+0

他不需要這個。看文檔。 https://docs.djangoproject.com/en/1.8/ref/models/fields/#django.db.models.ForeignKey他不需要導入模型。 –

+1

@ PauloPessoa-你是對的,我編輯了我的答案。 –