1

I'm following a django blog tutorial. When I used python manage.py shell and tried following commands in shell

from mainsite.models import Category, Tag
c = Category(name='category test')
c.save()

this error occured:

sqlite3.OperationalError: no such table: mainsite_category

This is my model.py in mainsite folder, which I think is correct:

from django.db import models
from django.contrib.auth.models import User
class Category(models.Model):
    name = models.CharField(max_length=100)

def __str__(self):
    return self.name

I have tried manage.py makemigrations, manage.py migrate and can successfully use manage.py runserver to see the website. So what is wrong here?

Edit: this is my INSTALLED APP in settings.py

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'mainsite',
]

And my project structure

|____djangoBlog
| |______init__.py
| |____settings.py
| |____urls.py
| |____wsgi.py
|____mainsite
| |______init__.py
| |____admin.py
| |____apps.py
| |____models.py
| |____tests.py
| |____urls.py
| |____views.py
|____manage.py
|____README.md
|____requirements.txt
|____templates
| |____blog
| | |____index.html

Edit: When I run python manage.py makemigrations and python manage.py migrate, the output is as follows:

No changes detected

Operations to perform:
    Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
    No migrations to apply.

3 Answers 3

2

UPD:

Since your project structure is lack of migrations folder in mainsite app, it means that you haven't created migrations for that app. Run python manage.py makemigrations mainsite and then python manage.py migrate.

From docs:

To add migrations to an app that doesn’t have a migrations directory, run makemigrations with the app’s app_label.

And yes, you can create model with simple instance creation. From docs Creating objects.

Original answer:

You need to be sure that you've included your 'mainsite' app in INSTALLED_APPS in settings.py and then run migrations. After that you should be able to do the following:

c = Category.objects.create(name='category test')
c.save()
Sign up to request clarification or add additional context in comments.

8 Comments

This doesn't work, the same error still occurs and it occurs earlier(Just after I enter c = Category.objects....
@Owen can you provide your project structure and part of settings.py with INSTALLED_APPS?
I edited the question. I wrote this program in another computer and use git to pull it to current computer.
@Owen you don't have migrations folder in your mainsite app, what it means is that you haven't created and run migrations of the app
I tried makemigrations and migrate but nothing happened(as edited). So what should I do to solve it?
|
1

@Owen, According to vishes_shell's answer, you'll have to do :

python manage.py makemigrations mainsite

the first time in order to generate a folder migrations that will contain your migrations, then you can just do

python manage.py makemigrations

In the futur if you add a new app, don't forget to make again the first command (changing the "mainsite" by the name of your app) before using the second one.

Comments

0

It seems like in your models.py you need an indentation at name:

class Category(models.Model):
    name = models.CharField(max_length=100)

2 Comments

I believe missing indentation is a consequence of copy-paste. Also you missed your indentation for str method.
Yes, me too. But I remember to have this kind of errors without any Django warning.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.