How can I remove this Error in Django models.py - django

I created a amount field in models.py,
I run python manage.py makemigrations and python manage.py migrate
It gave me error ValueError: Field 'amount' expected a number but got ''.
then I removed this amount field line from my code and again did makemigrations and migrate command. now again it it showing same error. when I open Django admin page and select my model name which is Orderss . It gives me error that there is no filed name amount
class Orders(models.Model):
order_id = models.AutoField(primary_key = True)
items_json = models.CharField(max_length=5000)
name = models.CharField(max_length=20)
# amount = models.IntegerField(default=0)
email = models.CharField(max_length=20)
address = models.CharField(max_length=50)
city = models.CharField(max_length=20)
state = models.CharField(max_length=20)
zip_code = models.IntegerField(max_length=6)
phone = models.IntegerField(max_length=12, default="")
def __str__(self):
return self.name

Delete your migrations and delete you db.sqlite3
and then
python3 manage.py makemigrations
python3 manage.py migrate
hope it work

When you did the migration first time, you’ve created the table in the database with ‘amount’ column. The second migration did’t remove this column. You can go to your database shell and remove the column there. It depends on the type of database you use, but you you might try to do it this way:
python manage.py dbshell
mysql> | psql> ALTER TABLE <table_name> DROP column <COLUMN_NAME>;
Then exit the shell and sync
python manage.py syncdb

1.In the Migration folder delete all files except __init__.py and then run
python manage.py makemigrations and
python manage.py migrate

Related

I can't makemigration appname in Django project

After completed, I pushed and rebase git master, database in my project is locked. I removed database and create new, but I don't see table for Model in models.py. How should I fix? Help me
models.py
class UserInfor(models.Model):
GENDERS = (
('nam', "Nam"),
('nữ', "Nữ"),
('khác', "Khác")
)
user = models.OneToOneField(User, on_delete=models.CASCADE)
date_of_birth = models.DateField(null=True)
gender = models.CharField(choices=GENDERS, max_length=10)
address = models.CharField(max_length=255, null=True)
phone_number = models.CharField(max_length=10, null=True)
def __str__(self):
return self.user.username
error
No changes detected
You should run python manage.py migrate to re-create your DB from scratch as you deleted it.
Run below command:
python manage.py makemigrations
Then:
python manage.py migrate
First make sure your app has been added to your project's settings.py
example:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
...................
...................
'yourappname'
]
If you've done that already, check and see if you've also registered your UserInfor model in your 'admin.py`
from .models import UserInfor
admin.site.register(UserInfor)
If all the above checks out, you should be able to run your migrations and see your model's table.
Try to delete db and files from migrations folder except init.py
then python manage.py makemigrations and python manage.py migrate

Django How to Get rid of Migration Errors

I am using Django ; and when I change a model , getting an error everytime .
I am changing only one field in model, and getting stupidly a lot of errors EVERYTIME.
django.db.utils.OperationalError: no such table:
or
django.db.migrations.exceptions.InconsistentMigrationHistory:
or
OperationalError no such column: table.colunm
or
django.db.utils.OperationalError: "Table already exists"
and bla bla bla..
I got maybe all error types in Django , and now it is bothering me really.
I am trying all solutions everytime :
Delete migrations
find . -path "/migrations/.py" -not -name "init.py" -delete
find . -path "/migrations/.pyc" -delete
Clear the migration history for each app
Remove the actual migration files.
Create the initial migrations
Fake the initial migration
python manage.py migrate --fake
python manage.py migrate --fake-initial
python manage.py migrate --run-syncdb
Drop database
Every solutions what i can find.
Stupidly trying all the solutions , and; at the last , yes i can find solutions BUT , I really really bored now from this stupidly errors.
Is there any way to get rid of migration errors in Django ?
Only I need the answer for this ; 'When I change a model field only, why am I getting these all madly errors, EVERYTIME ??!!!?'
For example :
this is my model :
from django.db import models
from django.conf import settings
from etahfiz.sabitler import DERS_SEVIYESI
# Create your models here.
class Student(models.Model):
systemId = models.CharField(max_length=15, unique=True )
adSoyad = models.CharField(max_length=20, blank=True)
dersSeviyesi = models.CharField(max_length=15,choices=DERS_SEVIYESI )
def __str__(self):
return str(self.systemId)
class Teacher(models.Model):
systemId = models.CharField(max_length=15 , unique=True)
user = models.OneToOneField(settings.AUTH_USER_MODEL)
adSoyad = models.CharField(max_length=20, blank=True)
def __str__(self):
return str(self.systemId)
class StuTeach(models.Model):
student = models.ForeignKey(Talebe)
teacher = models.ForeignKey(Hoca)
tarihBas = models.DateField()
tarihBit = models.DateField(blank=True, null=True)
This was working perfectly , BUT ; I wanted to add only one field to Teacher Model :
dersSeviyesi = models.CharField(max_length=15,choices=DERS_SEVIYESI )
Teacher model is like this now :
class Teacher(models.Model):
systemId = models.CharField(max_length=15 , unique=True)
user = models.OneToOneField(settings.AUTH_USER_MODEL)
adSoyad = models.CharField(max_length=20, blank=True)
dersSeviyesi = models.CharField(max_length=15,choices=DERS_SEVIYESI )
def __str__(self):
return str(self.systemId)
Aannd when I try to migrate :
python manage.py makemigrations sinif
python manage.py migrate
error error error
django.db.utils.OperationalError: "Table already exists"
Or sth like that..
Everytime that change only one field, getting all errors of Django...
How can I get rid of this type errors ??
Thank you.
Go to your database and find migrations table and also delete on entries. Then run migrations again. At this time you may face ContentType already exists. THen delete content_type table. Or, the easiest solution is to delete the database and create again, but if you have important data, all data will be lost.

Added another column to a model in Django, how do I update database?

So I changed the names and added a column to a model in django.
Before:
class MyApp(models.Model):
id = models.AutoField(primary_key=True)
aaa = models.CharField(max_length=100)
bbb = models.CharField(max_length=100)
After:
class MyApp(models.Model):
id = models.AutoField(primary_key=True)
first = models.CharField(max_length=100)
second = models.CharField(max_length=100)
third = models.CharField(max_length=100)
I made these changes in the MyApp's view, model, and serializer, however when I try to access my new API endpoint, it fails because the database doesn't contain the new column names. How can I update the database to reflect my new model? (I don't care about any of the data for this model, so I can wipe it). No idea how to do this
Make sure you run the commands
python manage.py makemigrations appname
python manage.py migrate appname
If you get table already exists run command
python manage.py migrate --fake appname
This solved the problem when I had it.
https://docs.djangoproject.com/en/1.11/topics/migrations/#initial-migrations

Django Model syncdb creating existing tables

In django model I had a class named Invitation.
I execute manage.py syncdb table Invitation is created.
This is my first table.
But when i create another class named Image and execute manage.py syncdb
it returns an error.
that Table UserInvitation already exists.
Please help me to create the table Image using syncdb command.
Models
class UserInvitation(models.Model):
InvitationID = models.AutoField(primary_key=True)
InvitationCode = models.CharField(max_length=255)
Email = models.CharField(max_length=150)
ExpireDate = models.DateTimeField()
Deleted = models.BooleanField(default=False)
CreatedDate = models.DateTimeField(auto_now=True)
class Meta:
db_table = u'UserInvitation'
class UserImage(models.Model):
ImageID = models.AutoField(primary_key=True)
UserID = models.IntegerField(unique=True)
FileName = models.CharField(max_length=255)
class Meta:
db_table = u'UserImage'
You should use South to reflect changes in your database.
syncdb runs commands that create all the tables in the DB, not just the one that you've added.
With South, you should revert your model back to when it only had the UserInvitation class, run the command:
python manage.py convert_to_south <name of app>
Add back the UserImage class and run the command:
python manage.py schemamigration --auto <name of app>
python manage.py migrate <name of app>
This would add the userimage table in your database.
Also, the db_table field in the meta classes should be in lowercase, as the table names, when created, are in lowercase (check your db to make sure)
See South documentation: http://south.readthedocs.org/en/latest/

Django South "myapp.foo" already exists" error with initial migration

I have an already existing app with alot of database entries.
class Foo(models.Model):
value = models.TextField(u"Value")
For this I do this:
python manage.py schemamigration myapp --initial
python manage.py migrate myapp
I change the model to such:
class Foo(models.Model):
value = models.TextField(u"Value")
live = models.BooleanField(u"Live", default=False)
creation_time = models.DateTimeField("Creation Time", auto_now_add=True, null=True, blank=True)
and migrate:
python manage.py schemamigration myapp --auto
python manage.py migrate myapp
I get django.db.utils.DatabaseError: relation "myapp.foo" already exists error.
I have already checked this question but --fake doesn't seem to be supported via South anymore.
Your models look invalid to me, though I'd be surprised if that's what's actually causing the problem.
It looks like your first argument is intended to be the verbose_name attribute, your model should probably look like this:
class Foo(models.Model):
value = models.TextField(verbose_name = u"Value")
live = models.BooleanField(verbose_name = u"Live", default=False)
creation_time = models.DateTimeField(verbose_name = u"Creation Time", auto_now_add=True, null=True, blank=True)
(you also forgot the u before the verbose_name for creation_time).
Meanwhile, --fake is definitely still supported (see the docs), what error are you getting when you try and run it?