Home > OS >  How can I intergrate my django app to an alredy made database, probably from a PHP app
How can I intergrate my django app to an alredy made database, probably from a PHP app

Time:11-17

this is part of the database. The extention is .sql

-- phpMyAdmin SQL Dump
-- version 3.2.0.1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: May 20, 2011 at 05:08 PM
-- Server version: 5.1.36
-- PHP Version: 5.2.9-2

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `bincomphptest`
--

-- --------------------------------------------------------

--
-- Table structure for table `agentname`
--

DROP TABLE IF EXISTS `agentname`;
CREATE TABLE IF NOT EXISTS `agentname` (
  `name_id` int(11) NOT NULL AUTO_INCREMENT,
  `firstname` varchar(255) NOT NULL,
  `lastname` varchar(255) NOT NULL,
  `email` varchar(255) DEFAULT NULL,
  `phone` char(13) NOT NULL,
  `pollingunit_uniqueid` int(11) NOT NULL,
  PRIMARY KEY (`name_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

I was creating a new database on django with mysqlite but I think I'm inexperience and this maybe possible

CodePudding user response:

To interpret these values in django you must look carefully at each field type so that you can create the variables that will store these values, such as name_id, firstname, lastname...

# settings.py 
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'django',
        'USER' : 'userid',
        'PASSWORD' :'password',
        'HOST' : 'localhost'
    }
}

Once the table has been defined in models and that you have added the respective app 'exampleApp.apps...' in settings.py, you will have already implemented mysql, list of available fields is here

import uuid
from django.db import models

class student(models.Model): 
    name_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    firstname = models.CharField(max_length=255)
    lastname = models.CharField(max_length=255)
    email  = models.EmailField(max_length=255)
    phone  = models.CharField(max_length=25)
    pollingunit_uniqueid = models.primary_key = True 

CodePudding user response:

Django can read legacy databases and even auto-generate the models for them,

connect to the db from settings and then run this command from your terminal

python manage.py inspectdb

it will print the models and you can use it as normal Django models

  • Related