Creating search form in admin panel - Django - python

I am new to Django.
I created the sqlite3 database called "test.db" and I'm not using the models.py or default database to sync in views.py for saving data.
I've created the HTML to get the value to store in test.db and print the data using the normal sqlite3 query.
The problem is that I have to access the test.db database in admin panel to search and print the data stored in the database. How to do this task?

You'll need to set up models.py for you to see data in admin functionality of the site. This is because the register statement register(*models, site=django.admin.sites.site) uses the models to display the data. If you are working on legacy database which you aren't, in this case, try this

Related

How to fetch data from postgres existing tables to Django?

I have a Postgres Database and it has plenty of tables in it, using Django I need to query or get all the table rows into my views.py. After reading I understood that creating a model in models.py will create a new table into my DB which is not what I need.
Is using django a good way to fetch data from postgres or should I use the python library to communicate with postgres directly (psycopg2)?
If django has a way to fetch data from table then how do I do it?
You can inspect the existing database schema by
python manage.py inspectdb
for adding the database model to models.py
python manage.py inspectdb > models.py

use existing database instead of django database

i want to connect my existing database of sql server with django but the problem is django has its model which create its own database but i dont want to create database using django i just want to use mine to retrieve data.
the one solution i saw was to use inspectdb but hte problem with inspectdb is that it does not pick wring keys and constraints sometime plus have to set many thing manually but in my project my database is user defined user will connect its database so i dont actually know that how many and what table user's database have do i just want to connect that database with django and use the value of it.
my existing database is sqlserver.
any technique to use my existing database without using django database and retrive data from existing database.
thankyou.
As you mentionned, you should use : inspectdb to create your models as stated in the doc (https://docs.djangoproject.com/en/4.0/howto/legacy-databases/)
Setting the managed option to False in the Meta class of model, you can instruct Django to not make any migrations or database schema modification, but you need to tweak the model yourself to ensure every kind of mapping is as you intendend, and obviousl ensure coherence between your DB schema and models.

View Django User table from MySQL console

I want to view Django default User table from MySQL console.
I know to access from django shell or python by simply importing it.
from django.contrib.auth.models import User
is there a way to view it from MySQL console itself?
and where it will be located? i mean in which database django user table belongs?
Of course you can see it in the database.
As with all other models, unless instructed otherwise by using a db_table property in the Meta class, Django uses the naming schema appname_modelname for tables - so in this case the table is auth_user.
If you don't know and can't find the source, you can ask the model itself - ie User._meta.db_table.
The tables are located in the database which you have specified in settings.py. The django user table will be located at yourdbname.auth_user. All user defined models will be stored as yourdbname.appname_modelname
You can use "python manage.py dbshell" (in order to do this in linux, you need to install mysqlclient) and use "show tables;" and "select * from yourdb.table"

New database refuses to show up in web2py appadmin

I am experimenting with a new blog database design and there are some tests I would like to run in web2py's administrative interface.
I started by creating a new web2py application called newblog from web2py's admin interface.
Next, I created newblog/models/appdb.py, below
Then I surfed to the admin interface at https://172.25.1.1/newblog/appadmin/index to ensure the database was created
I checked the filesystem and databases/newblog.db has a brand new creation time
I clicked through the appadmin menu to see my new database: "web2py" > "This App" > "Database"
Problem: The problem is I don't see it in the database admin interface for newblog. I have seen other empty web2py databases displayed in the appadmin interface, so I don't understand why mine does not show up there.
Question: Is this expected behavior? If so, what are the minimal steps I need to take for my web2py database to show up in appadmin?
"""
newblog/models/appdb.py
"""
def build_new_table():
return dict({'ugly_dict': 42})
db = DAL('sqlite://newblog.db')
## Build a table of tables, by the type of table (i.e. post, code, etc)
db.define_table('db_type',
Field('name', length=32, notnull=True, unique=True,
comment="Name of the database table"),
#IS_IN_DB(db, 'db.%s.name' % db.db_type.name)),
Field('database_pointer', notnull=True, unique=True,
compute=build_new_table(),
comment="Reference to the database table identified by 'name'",
),
)
## Define tags for the database items
db.define_table('tags',
Field('name', length=32, notnull=True, unique=True),
)
It sounds like you have the default db.py file in addition to your custom appdb.py file. Note, model files are executed in alphabetical order, so db.py is executed after your file. db.py assigns a different database connection to the variable db, so only that database is showing up in appadmin. You should either use the same database for both sets of tables, or use different variables for the two database connection objects. For example, in appdb.py, you might do:
blogdb = DAL('sqlite:\\newblog.db')
If you want to use the same database for all tables, then just define your DAL object in the first file (in this case, appdb.py), and you can refer to it in all subsequent model files (do not redefine it).

Migrate a legacy DB to Django, with image files

I have a SQL dump of a legacy DB, and a folder with images, and those are referenced by some rows of certain tables, and I need to migrate that data to the new Django models. The specific problem is how to "perform" the upload, but in a management command.
When the table with the field referenced is migrated to it's corresponding model, I need to also set the image field of the model, and I also need to process the filename accordingly to the upload_to parameter for the ImageField.
How to programmatically populate the image field from a file path or a file descriptor?
One approach would be to create a utility django project specifying your legacy database in settings.py. Then use the inspectdb management command to create a django model representation of your legacy database. And finally use dumpdata to get you data in JSON format.
You could then finally make your own JSON script that inserts your old data in your new models.

Categories

Resources