Syncdb not working - python

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

Related

Error: no such table when extending Abstract User Model

I wanted to extend the Base Abstract User Model within Django to have some other Fields:
class Student(AbstractUser):
birth = models.DateField(default=datetime.date.today)
street = models.CharField(max_length=20)
street_number = models.IntegerField(validators=[MinValueValidator(0), MaxValueValidator(99)])
city = models.CharField(max_length=20)
province = models.CharField(max_length=20)
code = models.IntegerField(validators=[MinValueValidator(0, MaxValueValidator(9999))])
address = str(street) + str(street_number) + str(city) + str(code) + str(province)
def __str__(self):
return f'Adresse: {self.address}'
I added this into my settings.py file:
AUTH_USER_MODEL = 'mainApp.Student'
But I get an Error saying there is no such table "Student" when trying to load /admin/.
I have made all the migrations using:
python manage.py makemigrations mainApp
python manage.py migrate mainApp
Error:
OperationalError at /admin/
no such table: mainApp_student
If y'all need any more infos, please comment!
Try in first time to run:
python manage.py makemigrations
python manage.py migrate
Without put the app name to migration process
You cannot define "address" like this. Database column is not for string concatenation of other columns, nor any calculation. It can cause error.

changes in models.py not reflecting on website even after running successful makemigrations and migrate commands

my models.py looks like this
from django.db import models
from django.urls import reverse
class Product(models.Model):
PRODUCTS_CATEGORY = [
('None', 'None'),
('Sterilisation', 'Sterilisation'),
('Cleaning Chemistry', 'Cleaning Chemistry'),
('Bacterial Barrier', 'Bacterial Barrier'),
('Waste Management', 'Waste Management'),
('Instrument Tracking', 'Instrument Tracking'),
('Validation', 'Validation')
]
prod_id = models.AutoField
prod_name = models.CharField(max_length=100)
prod_category = models.CharField(
max_length=100, choices=PRODUCTS_CATEGORY, default='None')
prod_desc = models.TextField()
prod_img = models.ImageField(upload_to='product_images')
slug = models.SlugField(null=True, blank=True)
def __str__(self):
return self.prod_name
i changed the spelling of Sterilisation to Sterilization (replacing the 's' with 'z')
i ran python manage.py makemigrations and it returned Migrations for 'website': website/migrations/0003_auto_20210407_1842.py - Alter field prod_category on product
i then ran python manage.py migrate and it returned Operations to perform: Apply all migrations: admin, auth, contenttypes, sessions, website Running migrations: Applying website.0003_auto_20210407_1842... OK
but i dont see the changes reflected on the website. the spelling still remains to be Sterilisation! What can be the reason for this?
PS: I am using sqlite3 and the website is on Ubuntu 20.04x64 which is a VPS.
The easiest solution for this was given by #azundo.
Step 1: Get into your python django shell by using the command python manage.py shell
Step 2: Import the desired model where you want to make the changes by using the command from <app_name>.models import <model_name>
Step 3: Use the below command
<model_name>.objects.filter(<column_name>='<old_spelling>').update(<column_name>='<new_spelling>')
In my case the above command was Product.objects.filter(prod_category='Sterilisation').update(prod_category='Sterilization')

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')

ValueError: invalid literal for int() with base 10: '' | Django

I'm using Django to build an ecommerce webapp. I wrote this code in models.py
from django.db import models
# Create your models here.
class Product(models.Model):
product_id = models.AutoField
product_name = models.CharField(max_length=50)
category = models.CharField(max_length=50, default="")
subcategory = models.CharField(max_length=50, default="")
price = models.IntegerField(default=0)
desc = models.CharField(max_length=300)
pub_date = models.DateField()
image = models.ImageField(upload_to='mainShop/images', default="")
Then, I performed makemigrations using
python manage.py makemigrations
which produced the following
D:\Projects\PyCharm Projects\VeroniCart\home>python manage.py makemigrations
No changes detected
Then I did
python manage.py migrate
This gave me the error:
ValueError: invalid literal for int() with base 10: ''
I'm also attaching a log file with the complete error.
Any help appreciated!
Issue solved. All I did was I deleted all files from my app's migrations directory (except migrations/init.py) and deleted the database file db.sqlite3 from the project directory.
Then I repeated the previous steps (makemigrations and migrate).
Django again created all migrations files and a new db.sqlite3 file, so this worked for me.

OperationalError:no such table:article_article

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.

Categories

Resources