0

learning django recently and ran into a problem with Django. This is my models.py:

# -*- coding:utf-8 -*-
#/usr/bin/env python
from django.db import models

# Create your models here.
class Article(models.Model):
    title = models.CharField(max_length = 100)
    category = models.CharField(max_length=50,blank=True)
    date_time = models.DateTimeField(auto_now_add=True)
    content = models.TextField(blank=True,null=True)

    def __unicode__(self):
        return self.title

    class Meta:
        ordering = ['date_time']

first I input these in cmd:

  1. python manage.py migrate
  2. python manage.py makemigrations
  3. python manage.py migrate

but when, in the Django shell, I input this code:

from article.models import Article
Article.objects.create(title = 'Hello World', category = 'Python', content = 'what')

I received this error message:

OperationalErrors:no such table:article_ article

what's wrong? thanks for your help

evbo
  • 157
  • 4
  • 14

2 Answers2

2

MAYBE something on migrations is not correct...

To create an app:

1) python manage.py migrate

2) python manage.py startapp myapp

3) add 'myapp', to INSTALLED_APPS in settings.py

4) create your model and save

5) python manage.py makemigrations myapp

6) python manage.py migrate myapp

You have to do the last two steps every time you change something in models.py.

Now some links: 1 2 and a very useful tutorial Django Girls

Community
  • 1
  • 1
Trix
  • 587
  • 1
  • 6
  • 27
1

You can simply erase you db by deleting your db.sqlite then ./manage.py syncdb.

If you don't want to loose your data then you need first, after making syncdb, run ./manage.py makemigrations, ./manage.py migrate. Than, after changing your models you run ./manage.py makemigrations and ./manage.py migrate -these command will make necessary changes to DB schema.

Dmitry Yudin
  • 1,033
  • 1
  • 10
  • 31