user7693832
user7693832

Reputation: 6859

Is there a better way to create model while the database already have the table

In my MySQL database I already have a table fo_dic.

mysql> desc fo_dic;
+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| id       | int(11)     | NO   | PRI | NULL    | auto_increment |
| name     | varchar(30) | YES  |     | NULL    |                |
| desc     | text        | YES  |     | NULL    |                |
| author   | varchar(20) | YES  |     | NULL    |                |
| priority | int(5)      | YES  |     | NULL    |                |
+----------+-------------+------+-----+---------+----------------+

Now I want to create the FoDic model by Django for Django Rest Framework to ORM the data.

from django.db import models

class FoDic(models.Model):

    id = models.IntegerField()
    name = models.CharField()
    desc = models.TextField()
    author = models.CharField()
    priority = models.IntegerField()

    def __str__(self):
        return self.name
    def __unicode__(self):
        return self.name

I want to know whether this will generate migrations when I sync the database, then there will have conflict? is there a way to create the Model as the same structure as the MySQL table?

Upvotes: 2

Views: 67

Answers (2)

ashkan haghju
ashkan haghju

Reputation: 162

You can add this property to your Meta class

Class Meta:
     managed=false

Add this your model class

Upvotes: 2

Mayank Porwal
Mayank Porwal

Reputation: 34086

If you already have database setup, you can automatically generate models from it:

python manage.py inspectdb > models.py

You don't need to worry about the conflicts in migrations with this as Django creates it itself.

For more details, please refer to this.

Upvotes: 1

Related Questions