I am new to Django, and I was given a complete MySQL database with all relations and tables set. I am trying to put this data from MySQL database into my admin site.
I want to know if there is any other way to retrieve data from MySQL database without using Django models.
MySQL Database
MySQL database
I tried to use autogenerated model, but it did not work for me. Data doesn't show up in the admin site. (P.S. I do not necessarily require data to be in admin site, I am just trying to use it any ways possible)
Auto-generated model by django
python manage.py inspectdb > models.py
admin.py
from django.contrib import admin
# Register your models here.
from .models import Ligand
admin.site.register(Ligand)
Please, let me know if you know any ways to use data from MySQL database that was not created using Django model.
Thanks in advance
python manage.py inspectdb can be run to detect and generate models. More on it here: https://docs.djangoproject.com/en/2.2/howto/legacy-databases/
Related
I am thinking of creating a project for which I chose 2 python frameworks, django and falcon. I will be using Falcon to build APIs and Django for other operations.I don't understand, how to manage database operations in both the frameworks which will be accessing single database(mysql database).
My plan is to create tables using Django models into the database, but how about accessing the database values in falcon. I am not much aware about sqlalchemy. Thanks in advance.
If you're dead-set on trying to access Django models from Falcon then this does look possible.
Apparently as long as your Django app's code is present wherever your Falcon app is, it looks like that you could use it as described in this question here:
import os
import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "app.settings")
django.setup()
from yourdjangoapp.things import *
I know this might be a sill question but I've been half an hour trying to figure this out and couldn't find anything :S
I have a django app and I significantly changed my database tables. I want to update my models.py file, but I tried the following commands and nothing happens.
syncdb, migrate, makemigrations...
I want to delete my previous models file and create a new one.
Thank you!
You got the workflow the wrong way around.
Django has an ORM that manages your database. If you want to make a change, you edit your models.py file. The migrations will automatically alter the database table to match your new model. It doesn't work the other way around: Django does not use database introspection to pick up manual changes in the database and edit your models file.
Now, there is a workaround, but it's not a long-term solution. In time you'll want to add custom functionality to your models, and you don't want to rewrite that functionality after each change. The introspection Django provides isn't perfect either, it's only meant as a tool to quickly start developing your application on top of a legacy database.
You can use manage.py inspectdb to generate the Django code for all existing tables. You can then copy the code for your specific model over to your models.py file. You should then delete the managed = False and db_table = ... options, remove any migrations, double-check the fields, and rerun makemigrations and migrate --fake-initial. This will get the database, your models, and your migrations back in sync, and then you'll be able to use the migrations framework for any additional changes.
Be sure to read the docs on migrations. That should leave you with a good understanding of how Django manages the database, and what the workflow is to make changes to your database.
I have a MySQL database, and want to have user accounting in my webpage. Honestly I don't want to write it from scratch. I know webapp2 have authentication but i don't know how to use it with MySQL database. Does anyone have any idea or have a suggestion?
This article explains in detail how to deal with user authentication using the webapp2_extras.auth module. It involves using the Datastore for user data. You’ll have to migrate your existing user base from MySQL db, but it’ll be a better choice for your use case than Cloud SQL.
So in my company we are slowly moving to Rails instead of PHP(Code Igniter to be precise).
So, our actual PHP App is using a Mysql DB and I'd like to connect a new Rails app to this DB but meanwhile our PHP is still running, so I can't change the DB.
I don't really know where I should start to use all the rails features (Or at least as much as possible).
There shouldn't be any harm in connecting your rails app to an existing database. You will need to watch for anything that goes against rails conventions (table names are plurals of models, for example) and either change the database (and your php app) or program around the problem in rails.
But the first step is simply to connect to the database and make models for the existing tables and see what works and what doesn't.
After that, post here with any specific problems.
As a suggestion, take a backup of your database and start out programming against that to build your application and be sure everything works safely.
Well, first of all you should setup the connection in config/database.yml and then start to generate the scaffolding (models, views and controllers) table by table (check the Rails generate command). I am not really sure if you have already generated the app though. Anyway, the generator will also generate a migration script that you obviously dont want to run as the db is already there.
Hope this helps a bit.
Anyway, some resources:
http://guides.rubyonrails.org/
http://railsapps.github.io/
There are two aspects of a Rails app to consider for this scenario:
1: the database connection
Simply put the credentials for this database into database.yml.
A model like "User" will by default attempt to find records and attribute definitions in a table called "users". ActiveRecord will assume there's an auto-incrementing integer primary key on each table. When saving records, it will attempt to write to columns called created_at and updated_at. Those are a few things to be mindful of when making and using the connection.
2: the database migrations
Rails uses migration files to manage a sequence of changes to the database structure. Under normal conditions, someone building a Rails app will be starting with an empty database.
In the case of an existing database, I would recommend making a migration something like:
class BuildLegacyDbStructure < ActiveRecord::Migration
def up
Mysql2.connection.execute_some_sql_file( # made-up function
Rails.root.join('path', 'to', 'file')
)
end
def down
# reverse those changes; bring DB down to blank state
end
end
Another option would be to disable Rails/ActiveRecord's migration-based management of the database entirely. For example Rails will generate a migration when you generate a new model. So if you have an existing users table in your PHP app, and you'd like to make a rails model to use this table, you'd run something like rails generate model User --no-migration.
My team is building a website in django.
We are using MySql and the database we created for the project is called 'vote'
We always share the code, but the problem is that whatever my project team has added to the database has to be added by me again,manually so as to use it.
Is there any way in which we can copy the whole database created by my team to my system?
Thanks
There are 3 approachs off the top of my head:
Export and Import the entire mysql database (using mysqldump or similar).
Use Django's fixtures system. This allows you to dump the contents of the DB to json/xml files which can be loaded again later by other members of the team via python manage.py loaddata .... These can be quite temperamental in reality and I generally find them more hassle then they are worth to implement.
Use South's data migrations. South is primarily concerned with managing schema migrations, i.e. gracefully handling the addition and deletion of fields on your models so that you don't end up with an inconstant DB. You can also use it to write data migrations which will allow you to programatically add data to the DB which you can share amongst your team mates.