Multiple Apps in one project with one database

346 views
Skip to first unread message

Bruckner de Villiers

unread,
Dec 11, 2019, 9:52:12 AM12/11/19
to django...@googlegroups.com

I got to generic views (CBV’s) in part 4 of the Django tutorials and decided instead of overwriting  the function views that I would get some practice by creating a new app (polls2).  The file structure is identical to the polls app, but obviously the code is different for the generic views and urls now pointing to polls2.  I ran migrate and makemigrations.  The makemigrations surprised me as it created Question & Choice tables again:

(pyfun) Bruckners-MacBook-Pro:mysite brucknerdevilliers$ python manage.py makemigrations

Migrations for 'polls2':

  polls2/migrations/0001_initial.py

    - Create model Question

    - Create model Choice

(pyfun) Bruckners-MacBook-Pro:mysite brucknerdevilliers$ python manage.py migrate

Operations to perform:

  Apply all migrations: admin, auth, contenttypes, polls, polls2, sessions

Running migrations:

  Applying polls2.0001_initial... OK

So, I looked at the folders and there is only one sqlite3 file.  The Database Structure is unchanged and the original data is still intact.  The admin page now displays 2 Question tables – the 2nd one contains no data.  Also, http://127.0.0.1:8000/polls2/ shows No Questions available.

 

 

 

I have probably messed up with models.py.  Any pointers please as how multiple apps can point to the same database?

Regards,

Bruckner de Villiers

+27(0)83 625 1086

Miracle

unread,
Dec 11, 2019, 10:26:56 AM12/11/19
to django...@googlegroups.com
Multiple Apps can point to one database.

What happens is that django creates different tables in the database.

It uses this format to create the tables

'appname_modelname'

e.g.  polls_question


Infact, if you  remember earlier on before runnung your first "python manage.py migrate".  Django told you that you had "unapplied migrations"

When you applied the migrations you might have noticed some other tables were created in the database. Tables like auth, session, admin, etc



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/63C629FA-DB68-4CC0-989D-1D346632CD94%40gmail.com.

Bruckner de Villiers

unread,
Dec 11, 2019, 12:41:04 PM12/11/19
to django...@googlegroups.com

OK, got it.  Changed polls2/views.py to “from polls.models import Choice, Question” instead of “from .models import Choice, Question”.  It works, but VSCode doesn’t like “polls.model” – it gives a warning “unresolved import”.

Much obliged,

 

Bruckner de Villiers

083 625 1086

cid:image001.png@01D5B019.54FE7190

 

I have probably messed up with models.py.  Any pointers please as how multiple apps can point to the same database?

Regards,

Bruckner de Villiers

+27(0)83 625 1086

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/63C629FA-DB68-4CC0-989D-1D346632CD94%40gmail.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages