python - single file Django, DRF project -
i trying bastardise django , django rest framework single module see if can work. far, have following code:
############################################################################### # settings ############################################################################### import os django.apps import apps django.conf import settings base_dir = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) if not settings.configured: settings.configure( debug=true, secret_key='thisisthesecretkey', root_urlconf=__name__, static_url='/static/', staticfiles_dirs=( os.path.join(base_dir, "static"), ), migration_modules = {'__main__': 'migrations'}, middleware_classes=( 'django.middleware.common.commonmiddleware', 'django.middleware.csrf.csrfviewmiddleware', 'django.middleware.clickjacking.xframeoptionsmiddleware', ), databases = { 'default': { 'engine': 'django.db.backends.sqlite3', 'name': 'tinydb', } }, installed_apps = ( '__main__', 'rest_framework', 'django.contrib.staticfiles', ), ) apps.populate(settings.installed_apps) ############################################################################### # models ############################################################################### django.db import models class book(models.model): isbn = models.autofield(primary_key=true) author = models.charfield(max_length=100) title = models.charfield(max_length=200) description = models.charfield(max_length=500) ############################################################################### # serializers ############################################################################### rest_framework import serializers class bookserializer(serializers.modelserializer): class meta: model = book ############################################################################### # views ############################################################################### class booksview(): queryset = book.objects.all() serializer_class = bookserializer ############################################################################### # urlconf ############################################################################### django.conf.urls import url, include rest_framework.routers import defaultrouter router = defaultrouter() router.register(r'books', booksview) urlpatterns = ( url(r'^$', include(router.urls)), ) ############################################################################### # manage ############################################################################### import sys if __name__ == "__main__": django.core.management import execute_from_command_line execute_from_command_line(sys.argv)
right now, server runs , see api browser. however, when try create object, following trace:
>>> __main__ import book >>> book.objects.create(author='a1', title='t1', description='d1') traceback (most recent call last): file "<console>", line 1, in <module> file "/home/lwm/.virtualenvs/tiny-api/lib/python3.4/site-packages/django/db/models/manager.py", line 127, in manager_method return getattr(self.get_queryset(), name)(*args, **kwargs) file "/home/lwm/.virtualenvs/tiny-api/lib/python3.4/site-packages/django/db/models/query.py", line 348, in create obj.save(force_insert=true, using=self.db) file "/home/lwm/.virtualenvs/tiny-api/lib/python3.4/site-packages/django/db/models/base.py", line 734, in save force_update=force_update, update_fields=update_fields) file "/home/lwm/.virtualenvs/tiny-api/lib/python3.4/site-packages/django/db/models/base.py", line 762, in save_base updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields) file "/home/lwm/.virtualenvs/tiny-api/lib/python3.4/site-packages/django/db/models/base.py", line 846, in _save_table result = self._do_insert(cls._base_manager, using, fields, update_pk, raw) file "/home/lwm/.virtualenvs/tiny-api/lib/python3.4/site-packages/django/db/models/base.py", line 885, in _do_insert using=using, raw=raw) file "/home/lwm/.virtualenvs/tiny-api/lib/python3.4/site-packages/django/db/models/manager.py", line 127, in manager_method return getattr(self.get_queryset(), name)(*args, **kwargs) file "/home/lwm/.virtualenvs/tiny-api/lib/python3.4/site-packages/django/db/models/query.py", line 920, in _insert return query.get_compiler(using=using).execute_sql(return_id) file "/home/lwm/.virtualenvs/tiny-api/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 974, in execute_sql cursor.execute(sql, params) file "/home/lwm/.virtualenvs/tiny-api/lib/python3.4/site-packages/django/db/backends/utils.py", line 79, in execute return super(cursordebugwrapper, self).execute(sql, params) file "/home/lwm/.virtualenvs/tiny-api/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute return self.cursor.execute(sql, params) file "/home/lwm/.virtualenvs/tiny-api/lib/python3.4/site-packages/django/db/utils.py", line 97, in __exit__ six.reraise(dj_exc_type, dj_exc_value, traceback) file "/home/lwm/.virtualenvs/tiny-api/lib/python3.4/site-packages/django/utils/six.py", line 658, in reraise raise value.with_traceback(tb) file "/home/lwm/.virtualenvs/tiny-api/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute return self.cursor.execute(sql, params) file "/home/lwm/.virtualenvs/tiny-api/lib/python3.4/site-packages/django/db/backends/sqlite3/base.py", line 318, in execute return database.cursor.execute(self, query, params) django.db.utils.operationalerror: no such table: __main___book
makemigrations
returns:
lwm$ python api.py makemigrations no changes detected
i can run migration:
lwm$ python api.py migrate operations perform: synchronize unmigrated apps: __main__, staticfiles, rest_framework apply migrations: (none) synchronizing apps without migrations: creating tables... running deferred sql... installing custom sql... running migrations: no migrations apply.
so. think, since don't have book
model in seperate app, there no database table being created it. other manually creating tables, example, using db_table
meta field, still wanted goodness of orm doing things me.
any ideas?
try removing __main__
list of installed apps. unless have app named __main__
(which shouldn't, given double underscores means in python), that's not supposed there.
Comments
Post a Comment