Django 1.9 CharField length modifications are not applied - python

I'm using django 1.9 and the admin site for cataloging.
I changed this in my models.py:
localidad = models.CharField(max_length=30, verbose_name='Localidad')
for this:
localidad = models.CharField(max_length=300, verbose_name='Localidad')
I saved models.py and made manage.py makemigrations and migrate but changes are not reflected, an error shows up saying the field has 30 character max.
Any help any help would be appreciated

Related

Python Migration

I have below code in Models.py
from django.db import models
class Post(models.Model):
text = models.TextField()
I have entered 100 around records to application which run using SQLite DB and Modified it in to following and migrate
from django.db import models
class Post(models.Model):
title = models.CharField(max_length=200)
author = models.ForeignKey("auth.User",on_delete=models.CASCADE,)
text= models.TextField()
When I try to migrate the changes using python manage.py makemigrations I am getting message regarding the 100 records that have null for author. What I need is to modify
above code to set any text without author must be setup as superuser by default.

Custom User model changes never migrate

I made the mistake of not using a custom user model for the first migration and I paid the price by having to delete all the migrations and makemigrations and migrate again as I switched the project to use my custom user.
models.py
"""
Changes are never picked by makemigrations
"""
from django.contrib.auth.models import AbstractUser
from django.db import models
class User(AbstractUser):
username = models.CharField(max_length=120, unique=True, blank=False,
validators=[MinLengthValidator(2)])
email = models.EmailField('Email Address', unique=True, blank=False)
FCM_ID = models.CharField(max_length=300, blank=True)
def __str__(self):
return self.username
Everytime I made any changes to the custom user, I have had to delete all migrations related files and apply migrations again just to get past the errors- this SO answer and this.
Now I realized that I needed a newer change, but migrations are not detected after changing the User model. The only solution I have is to delete the migrations again! So far I have done this chore like 5 times and I am tired, what am I doing wrong?
I have tried python3 manage.py makemigration <app_name>, and of course, the custom user model is defined in settings.py using AUTH_USER_MODEL.

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.

Django AbstractEmailUser model column does not exist

I created a CustomUser model, inheriting from AbstractEmailUser.
I wanted to add an avatar field, after finishing it and making migrations but I'm getting the following error:
column account_customuser.avatar does not exist
LINE 1: ...user"."name", "account_customuser"."valid_email", "account_c...
models.py looks like this now
class CustomUser(AbstractEmailUser):
nickname = models.CharField('nickname', max_length=100, unique=True)
name = models.CharField(max_length=200, blank=True, null=True, default=None)
valid_email = models.BooleanField('valid email', default=False, blank=True)
avatar = models.ImageField(upload_to='profile/photo', blank=True, null=True, default=None)
What can I do to correctly add the avatar field?
As stated here: Django Programming error column does not exist even after running migrations
Something may have gone wrong in your migration process.
Go to your database and find a table named django_migrations where
all the migrations are listed.
Find the row with the migration in which you added the avatar column to your model and delete it from the database (only the row).
Migrate again: ./manage.py migrate
Another possibility is that you are using Django Toolbar like what happened here: Django Migration Error: Column does not exist, in which case you need to comment the toolbar in your installed apps and rerun migrations.
Did you apply a new migration with these changes?
You can check this using showmigrations or use makemigrations to create a migration and migrate to apply it.

Django migration doesn't always work

Ok So I have some migration issue in django 1.8 and I need to work around by manually dropping my DB table every time.
My problem is following - every time after I change my table by adding new fields and running
python manage.py makemigrations
python manage.py migrate
it says no changes to apply. (migrate folder is empty)
(It is not picking up changes I made in model file )
At the end table stays with old structure and it gives me errors when I test.
If I drop table in DB directly and start again it works but it is annoying since I have to recreate a test data every time.
Is it a bug in migration or just me ?
For example this is my table from models file but it happened before with other tables.
#with_author
class BOM(models.Model):
name = models.CharField(max_length=200,null=True, blank=True)
description = models.TextField(null=True, blank=True)
product= models.ForeignKey(Product, on_delete=models.PROTECT)
material = models.OneToOneField(Material, related_name = 'material')
creation_time = models.DateTimeField(auto_now_add=True)
materialuom = models.CharField(max_length=1,
choices=UOM_CHOICES)
quantity = models.DecimalField(max_digits=19, decimal_places=10)
waste = models.DecimalField(null=True, blank=True,max_digits=19, decimal_places=10)
def __unicode__(self):
return u'%s %s' % ( self.id, self.name)
Ok so I got my work around thanks to comment from #ahmed.
Every time when doing python manage.py makemigrations appname it is mandatory to type the appname .Without the appname functionality is not always working.
However I believe there is still problem in django1.8 migrate process.

Categories

Resources