OperationalError:no such table:article_article - python

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:
python manage.py migrate
python manage.py makemigrations
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

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

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.

Related

Django, makemigrations doesn't detect model changes

I am learning Django and I am using this tutorial:
https://www.youtube.com/watch?v=F5mRW0jo-U4&t=912s
I created an app called products and I have it in INSTALLED_APPS.
Upon creating a Product class in the models.py file in the products app folder, I created some fields such as price.
However, when I tried to use the makemigrations command, no changes detected was reported.
Folder Layout and Settings
models.py
from django.db import models
# Create your models here.
class Product(models.Model):
title = models.TextField()
description = models.TextField()
price = models.TextField()
I checked if there were any issues with the Product class but there doesn't seem to be any as far as I can see, so I am at a loss as to why no changes were detected.
15
You may have deleted the migrations folder inside the app or init.py inside the /migrations/ folder, create a new one
myproject/ apps/
myapp/
migrations/
init.py
You can always do makemigrations seperately without doing the above step
python manage.py makemigrations myapp
Refer the table - djangomigrations .. if the entry is there for the makemigration file already in the table then trunckage and rerun makemigrations and then do migrate

Error in django when I add two new fields to a class model

I have a class called Difference in my script "myProject.models.difference.py" that is:
class Difference(models.Model):
comparator = models.ForeignKey(ScenarioComparator, on_delete=models.CASCADE)
summary = models.CharField('summary',max_length=900000)
And in my script "myProject.admin.scenario.py" I have the corresponding admin class:
class DifferenceAdmin(admin.ModelAdmin):
list_display = ("comparator","summary",)
But I need to add two fields more to my Class Difference:
class Difference(models.Model):
comparator = models.ForeignKey(ScenarioComparator, on_delete=models.CASCADE)
summary = models.CharField('summary',max_length=900000)
diff_class = models.CharField('diff_class',max_length=1000)
diff_field = models.CharField('diff_field',max_length=500)
After that I read the next error: "no such column: myproject_difference.diff_class".
But if I comment the new fields diff_class, diff_field of this way:
class Difference(models.Model):
comparator = models.ForeignKey(ScenarioComparator, on_delete=models.CASCADE)
summary = models.CharField('summary',max_length=900000)
#diff_class = models.CharField('diff_class',max_length=1000)
#diff_field = models.CharField('diff_field',max_length=500)
Then the error disappears. ¿What must I do in order to add the new fields?
You need to do a database migration. Save your model:
class Difference(models.Model):
comparator = models.ForeignKey(ScenarioComparator, on_delete=models.CASCADE)
summary = models.CharField('summary',max_length=900000)
diff_class = models.CharField('diff_class',max_length=1000)
diff_field = models.CharField('diff_field',max_length=500)
Then do the following two manage.py commands:
$ python3 manage.py makemigrations
$ python3 manage.py migrate
You will need to do this every time you make changes to models.py
After adding fields to your model, you must make migrations to add the columns to your database
run these two commands
python manage.py makemigrations
python manage.py migrate
In the myProject.models folder, is there an __init__.py file? Inside of it, you'll need this:
from .difference import *
When you have models in a different .py file than models.py, you need to import them in the init.py file, or else the Django app will not know that it's there.
When you did python manage.py makemigrations did it create migrations files in the migrations folder?
The solution is to write a defoult value for diff_class and diff_field. When you run python manage.py makemigrations it appears a warning saying you need defoult values.
So I write
class Difference(models.Model):
comparator = models.ForeignKey(ScenarioComparator, on_delete=models.CASCADE)
summary = models.CharField('summary',max_length=900000)
diff_class = models.CharField('diff_class',max_length=1000,default = 'class')
diff_field = models.CharField('diff_field',max_length=500, default= 'field')

Django db migration stops working with "Meta" class

I'm trying to create a Django application, and I have a class like this in my models.py file:
class Identity(models.Model):
display_name = models.CharField(db_column = "DisplayName", max_length = 200)
When I run python manage.py makemigrations myApp, everything works as expected, and a migration file for the class is created.
The problem happens if the class in the model defines a Meta class as well:
class Identity(models.Model):
display_name = models.CharField(db_column = "DisplayName", max_length = 200)
class Meta:
app_label = "something"
Now, if I run the makemigrations command, I get a "no changes detected" message, and no migration file is generated:
> python manage.py makemigrations myApp
No changes detected in app 'myApp'
Please note that this was just an example, and the problem happens also if I run the command for the first time on a class with a Meta class defined, or if I delete the previously generated migration files.
I'm using Python 2.7.5 and Django 1.9.5 on Windows.
You don't have this in the Meta class do you?
managed = False

Syncdb not working

from django.db import models
from imagekit.models import ImageSpecField
from imagekit.processors import ResizeToFill
class Product(models.Model):
class Meta():
db_table = 'tovar'
product_title = models.CharField(max_length=200)
product_img = models.ImageField(upload_to='images')
avatar = models.ImageField(upload_to='avatars')
avatar_thumbnail = ImageSpecField(source='avatar',processors=[ResizeToFill(100, 50)],format='JPEG',options={'quality': 60})
product_categories = models.ForeignKey(Category)
product_subcategories = models.ForeignKey(Subcategory)
product_description = models.TextField()
def __unicode__(self):
return self.product_title
profile = Product.objects.all()[0]
print(profile.avatar_thumbnail.url)
print(profile.avatar_thumbnail.width)
Dont working manage.py syncdb
Error:
django.db.utils.OperationalError: no such table: tovar
pls healp me
'python manage.py syncdb' doesn't work for Django 1.9 or later, try this code!
python manage.py makemigrations
python manage.py migrate
python manage.py migrate --run-syncdb
I have the same problem with Django 1.9 and 1.10. This code works!
If you have modified your model you first have to run
manage.py schemamigration your_django_application_name --auto
Then you run:
manage.py migrate your_django_application_name
use the below command
>>python manage.py sqlmigrate application_name 0001
if you have more tables then increment the number to ooo2 and run the abe code again
>>python manage.py runserver

django python manage py syncdb doesn't add tables to mysql

I just started learning Django yesterday, and I'm having a problem when I trying to add new tables to my MySQL server:
I created a model in models.py (lets say the app name is "article" and the model name is "Article"). I ran syncdb lots of times with different examples in the INSTALLED_APPS in the settings.py (like "article" and "article.models") but it doesn't add the tables.
(Of course I see the other tables like "auth_group", "auth_group_permissions" etc.. but not the table of the Article model I created.)
model.py:
from django.db import models
class Article(models.Model):
title = models.CharField(max_length=200)
body = models.TextField()
pub_date = models.DateTimeField('date publish')
likes = models.IntegerField()
the project structure:
python version: 2.7.9
the db:
The documentation for the new Django migrations is available here:
https://docs.djangoproject.com/en/dev/topics/migrations/
once you have your new app and added it to INSTALLED_APPS in settings.py, you do the following:
python manage.py makemigrations
this will create a python file in the app migrations folder. It will realize the changes, new additions...etc to apply to your database and then you run
python manage.py migrate
no more syncdb, it will be obsolete soon

Categories

Resources