I am looking to have a django app that visually displays a SQLite database. The idea is that the user would pass in the path to the sqlite file in the URL for the app. I was wondering if I could make use of the Django models and use something like django-tables2 to display it. I was just wondering if the Models are not setup in models.py prior to the start of the server.
Is there a way to still use Model, read/write to the given database and be able to still build the table?
Related
I am working on already existing data on relational database. Now question is that how to build models and how to update the tables with new data coming from user (technically django forms)?
Django natively supports creating models for and working with existing data. From the documentation:
Integrating Django with a legacy database
Django will still need to create several of its own tables, but will adapt to use your existing tables. From the doc, you can auto-create models like this:
python manage.py inspectdb > models.py
You'll need to determine whether you want to manage updates to the table structure, but that's getting into details that will be specific to your project.
I built a small django app and created some models. Now I would like to populate the database (the tables and rows do already exist) using a python script since I have to read the information I want to populate the database with, from multiple external files. Is there any way to do that?
EDIT:
Python 3.7
Django 3.0
You can always use any python routine to read files, process the content, create your model instances and save them the to the database by using a custom Django management command.
check this out: how to import csv data into django models
I spin up a django project. Afterwards, i didn't write models.py but instead I created a database from MySQL command line(independent from django) and created three tables with required columns. Finally i connected my django app with that database successfully. I applied migrations. But now i am confused do i need to write models.py with every field name as in column?
I remember implementing a basic project in which i did write models.py and created database using "python manage.py shell" and then put values using
"from polls.models import Choice, Question"? How do i put data now initially and then using python on some action from UI?
Do i need models.py even for ready made mysql databases?
You do not need to construct models. Some (small) webservers are even completely stateless, and thus do not use a database. But a large part of how Django can help you is based on models.
You can write your own queries, forms, etc. But often by using a ModelForm, Django can for example remove a large amount of boilerplate code. It will make it furthermore less likely that there are mistakes in your code. So although not strictly necessary, the models are usually a keystone in how Django can help you.
You can use the inspectdb [Django-doc] command to inspect the database, and let Django "sketch" the models for you. Usually you will have still some work. Since Django can, for example, not derive that a field is an EmailField, since both a CharField and EmailField look exactly the same at the database side.
You do not need to use inspectdb however. You can construct your own models. If you create your own models, but these exist already at the database side, you might want to set managed = False [Django-doc] in the Meta of your model, to prevent Django from constructing migrations.
I am working on a project that requires connecting to an existing remote Oracle database. the problem is that the client has not given full access to the database. instead i'm only given accesss to some views(Relational Database View) of the database. the table names are not provided.
I'm able to perfrom raw sql queries to the legacydb in my django view.
But it would be very much more easier if i could generate the models for these views and use django ORM. is there any way to accomplish this? What I'm expecting is to use some commands like inspectdb to generate the models from the view provided by the oracle legacy database.
In a django app, I have two postgresql databases connected through settings.py: one is default and other is AppDb. AppDb is placed on a remote machine.
I want to query from a 'Courses' model from AppDb using 'using()' and 'Courses' model is not available in default database.
So my query goes like this:
courseInfo = Courses.objects.using('AppDb').filter(cuser_id = 12)
But I am getting NameError for 'Courses'
Can I have a solution for such queries without using routers
If you have an existing database, you still need to create an app and models for that database in order to use the ORM.
To help you create the model classes, you can use the inspectdb management command which will try to guess the models from an existing database and create the models.py for you. Its not perfect, but it will save you some time.
You will still have to make sure the models have a primary key and are written in the correct order (so that foreign keys will work correctly).