Push database into Django - python

I'm currently working on a project with Django, I have designed a model like
Class Item(models.Model):
id
name
...
And I've already had a sqlite database with data like
Id, name, ...
1, a, ...
2, b, ...
Now, the question is how I can push this database to django? Thanks

You usually don't create any databases tables in the database per se, you just need to create the Django models and then run and apply the migrations. So, for instance, if you have the following model:
class Item(models.Model):
name = models.CharField(max_length=255)
You just need to go to the terminal and type
python manage.py makemigrations (This will create a migration file)
python manage.py migrate (This will create the actual tables/columns in your database)
If you then open a shell:
python manage.py shell
and create an object via Django:
from yourapp.models import Item
Item.objects.create(name='Hello')
That Item should be saved to your DB.

Related

How can one display contents of a database to a template from a non Django project [duplicate]

I have an existing database I'd like to use with Django. Can I create models based on my existing tables?
This is documented on the Django website:
https://docs.djangoproject.com/en/3.1/howto/legacy-databases/
$ python manage.py inspectdb
You can use inspectdb command.
Like this
python manage.py inspectdb Table_Name --database=DataBaseName >
filename.py
eg: For specific table from specific database()
python manage.py inspectdb Employee_table --database=db_for_employee > models_file.py
Here db_for_employee exist in DATABASE list in settings.py file.
For specific table in default database:
python manage.py inspectdb Employee_table > models_file.py
For all the tables in default database:
python manage.py inspectdb >models_file.py
This would create a file with name models_file.py at your project level and it would contain the models for the existing database.
Note that the if you don't mention the database name then default database from the settings would be considered.
And if you don't mention the table name then all the tables from the database are considered and you'll find models for all the tables in new models.py file
Needless to say that further you'll have to copy the models or the classes created, to the actual models.py file at the application level.

Create a new table in the view.py in Django

In the document, I can define a table in the models.py.
from django.db import models
class Person(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
Run python manage.py makemigrations and python manage.py migrate to create a new table in the database.
If I want to create a new table when do something in the views.py, is it possible?
For example, I want create a personal table for some new member when he/she register a new account.
this is not suggested, standard and neither possible. the tables will be referenced over migrations and will be created by model classes. any table which violate these rules will make you trouble in the future

Find Django Database in the PC

I am trying to create a django project and I created a simple model and ran the django server, entered the fields. I can see all the data on the django server. I was wondering if there is any other way to see the data entered.
This is the data I entered. Can I view it anywhere else in tabular form or database form?
This is what I do in command window:
sqlite3 db.sqlite3
followed by
.tables
Where db.sqlite3 is the file that was created when I migrated. I am not sure if the data I entered on django server is in this file.
Thanks in advance.
EDIT
This is my models.py:
class Site(models.Model):
# Site ID
siteID = models.CharField(max_length=255, null=False)
# End Device ID
edevID = models.CharField(max_length=255, null=False)
After running python
manage.py runserver
I am directed to the django page where I enter the following data
And I save this. My question is how can I access this saved data in a database. I tried what you recommended Satya, it only shows me the superuser id.
I hope the question is clear now.
You can install this software. After installation, select your sqlite and see your complete database.
DB Browser for SQLite
Download from here: DB Browser
The question doesn't give enough information as to what you're trying to look for. I am assuming that you want more information out of the tables you've created and that you are using the default sqlite3 database. In your models.py file, you can use the special __str__ method to make your objects more descriptive
def __str__(self):
return self.title #if your using 'title' as the attribute to identify your objects
Also,
enter this command in cmd
python manage.py shell
and then in the shell, import the Model whose database you want to view
for e.g.
>>> from django.contrib.auth.models import User
>>> User.objects.all()
This will display all the data associated with your table. Unfortunately, not in tabular form. You can aso use the command python manage.py inspectdb to view all the classes associated with all tables. To view in proper tabular form, you might need phpmyadmin.

Declaratively set permissions on Postgres table via Django Model?

We have two different groups working on the same Django database:
The web team
A team of data scientists
We'd like to make it easy for the data scientists to read/write to a subset of a tables using Django models but without giving them write/delete access to the rest of the tables.
Our current thought is that we'd like to lock down the tables in the data layer (Postgres) with GRANT and REVOKE style SQL statements, but we'd like to manage those permissions in the models.
We'd have two ROLES:
data_scientists_rw
web_team_admin
Instead of manually writing GRANT and REVOKE permissions in each migration, we'd like to have a decorator or a Meta class variable on a model so that when we makemigrations, it will automatically generate the correct SQL.
# data_scientists/models.py (pseudocode)
class DataScientistModel(models.Model):
...
my_special_number = models.FloatField()
...
class Meta:
data_science_team_editable = True
Make the migration:
$ ./manage.py makemigrations
Auto-generated model (pseudocode):
# migrations/0008_new_data_scientist_model.py
...
if data_science_team_editable:
RunSQL('GRANT INSERT, UPDATE, DELETE ON {tablename} TO data_scientists_rw;')
...
Questions:
Does this approach seem sensible?
How I do hook into makemigrations so that I can auto-generate the RunSQL code?

sqlite configuring with django

I have been assigned the task of setting up an online database with sqlite via django- where it will contain columns of information. My problem is that I can't figure out which is establishing table where the information will go- my models.py script or should I do it with a "CREATE TABLE" command?
An example of the code in my models.py script is below:
class Person(models.Model):
firstname=models.CharField(max_length=100)
lastname=models.CharField(max_length=100)
The tutorial that I was given recommended this command:
insert into freezer_person (firstname,last name) values('Louis','Pasteur')
However upon executing this command I naturally got an error saying "table does not exist" i.e. I have to use the "CREATE TABLE" command.
so when I tried:
"CREATE TABLE in freezer_person(...)"
It returned that "in" was syntactically incorrect.
So my question is how do I go about creating a table that will intake info from models.py?- I can't seem to find any info on this...
You don't create the tables; Django does it for you, through the migrations system.
This is all fully covered in the tutorial.
Your model classes from models.py define your tables: each model class will be transposed in a table. Each property of a model class will be a column in the corresponding table. Each instance of the model class will be a row in that table.
So when you want to create a table you define a model class in the models.py file of your app, then run
python manage.py makemigrations
which tracks the changes made to the model class and generate a migration file which contains the sql statements to be applied to the database, and then to apply them to the database, you run
python manage.py migrate

Categories