开发者

How do I get syncdb db_table and app_label to play nicely together

开发者 https://www.devze.com 2023-01-17 18:16 出处:网络
I\'ve got a model that looks something like this: class HeiselFoo(models.Model): title = models.CharField(max_length=250)

I've got a model that looks something like this:

class HeiselFoo(models.Model):
    title = models.CharField(max_length=250)

    class Meta:
        """ Meta """
        app_label = "Foos"
        db_table = u"medley_heiselfoo_heiselfoo"

And whenever I run my test suite, I get an error because Django isn't creating the tables for that model.

It appears to be an interaction between app_label and db_table -- as the test suite runs normally if db_table is set, but app_label isn't.

Here's a link to the full source code: http://github.com/cmheisel/heiselfoo

Here's the traceback from the test suite:

E
======================================================================
ERROR: test_truth (heiselfoo.tests.HeiselFooTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/chris/Code/heiselfoo/ve/lib/python2.6/site-packages/heiselfoo/tests.py", line 10, in test_truth
    f.save()
  File "/Users/chris/Code/heiselfoo/ve/lib/开发者_JAVA技巧python2.6/site-packages/django/db/models/base.py", line 434, in save
    self.save_base(using=using, force_insert=force_insert, force_update=force_update)
  File "/Users/chris/Code/heiselfoo/ve/lib/python2.6/site-packages/django/db/models/base.py", line 527, in save_base
    result = manager._insert(values, return_id=update_pk, using=using)
  File "/Users/chris/Code/heiselfoo/ve/lib/python2.6/site-packages/django/db/models/manager.py", line 195, in _insert
    return insert_query(self.model, values, **kwargs)
  File "/Users/chris/Code/heiselfoo/ve/lib/python2.6/site-packages/django/db/models/query.py", line 1479, in insert_query
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/Users/chris/Code/heiselfoo/ve/lib/python2.6/site-packages/django/db/models/sql/compiler.py", line 783, in execute_sql
    cursor = super(SQLInsertCompiler, self).execute_sql(None)
  File "/Users/chris/Code/heiselfoo/ve/lib/python2.6/site-packages/django/db/models/sql/compiler.py", line 727, in execute_sql
    cursor.execute(sql, params)
  File "/Users/chris/Code/heiselfoo/ve/lib/python2.6/site-packages/django/db/backends/sqlite3/base.py", line 200, in execute
    return Database.Cursor.execute(self, query, params)
DatabaseError: no such table: medley_heiselfoo_heiselfoo

----------------------------------------------------------------------
Ran 1 test in 0.004s

FAILED (errors=1)
Creating test database 'default'...
No fixtures found.
medley_heiselfoo_heiselfoo
Destroying test database 'default'...


Ok, I'm a moron. You need to have an app in INSTALLED_APPS that matches your app_label or your tables won't get created.


You need to have an app in INSTALLED_APPS that matches your app_label or your tables won't get created.

In my case it doesn't work - when I change app name in INSTALLED_APPS on app_label wchich matches models Meta setting, but is different than app folder name, manage.py syncdb raise: "Error: No module named ...". I still have no idea how to sync models with database in case of using app_label and db_table in model Meta.


Caveat: this is not a direct answer to your question.

From looking at your code in github I find that you have defined the model inside models.py. And yet you have defined app_label inside your model's Meta. Is there a reason for doing this?

Django's documentation says that app_label is meant to be used

If a model exists outside of the standard models.py (for instance, if the app’s models are in submodules of myapp.models), the model must define which app it is part of:

Given that, I don't see any reason to define an app_label in your case. Hence my question. Perhaps you are better off without defining the app_label after all.

0

精彩评论

暂无评论...
验证码 换一张
取 消