difficulty in updating csv file - python

I tried to update the CSV file with a description column. I tried to add an update to the model by adding description = models.CharField(max_length=200, default='SOME STRING') like this:
wine = models.ForeignKey(Wine)
pub_date = models.DateTimeField('date published')
user_name = models.CharField(max_length=100)
comment = models.CharField(max_length=200)
rating = models.IntegerField(choices=RATING_CHOICES)
description = models.CharField(max_length=200, default='SOME STRING')
When I run migrate for this change I get the following error:
c:\Users\Amira Joshi\Desktop\winerama>python manage.py makemigrations
Migrations for 'reviews':
reviews\migrations\0002_review_description.py
- Add field description to review
How can I solve it? Please help!

This is not an error:
c:\Users\Amira Joshi\Desktop\winerama>python manage.py makemigrations
Migrations for 'reviews': reviews\migrations\0002_review_description.py
- Add field description to review
With this django says that the migration file is created, now you must apply the migration by running this command:
python manage.py migrate

Related

Profile table not being created for extended User model in django

I am facing this problem in django where even though there is a Profile model in my models.py file which extends the django User model still on running the 'makemigration' and 'migrate' commands the Profile table is not being generated in database.
This is my models.py:
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
clg_name = models.CharField(max_length=200)
class Student(models.Model):
name = models.CharField(max_length=100, blank=False)
enrollment_number = models.IntegerField(unique=True, null=True, blank=False)
clg_name = models.CharField(max_length=200, blank=False)
program_name = models.CharField(max_length=30, blank=False)
class Result(models.Model):
student = models.ForeignKey(Student, on_delete=models.CASCADE)
sem = models.IntegerField()
exam_name = models.CharField(max_length=30)
percnt = models.FloatField(null=True)
cgpa = models.FloatField(null=True)
class Marks(models.Model):
result = models.ForeignKey(Result, on_delete=models.CASCADE)
course_name = models.CharField(max_length=100, null=True)
course_code = models.IntegerField(null=True)
course_credit = models.IntegerField(null=True)
grade = models.FloatField(null=True)
Here is the output of py manage.py makemigration account:
Migrations for 'account':
account\migrations\0001_initial.py
- Create model Result
- Create model Profile
- Create model Marks
And this is the output of py manage.py migrate:
Operations to perform:
Apply all migrations: account, admin, auth, contenttypes, sessions
Running migrations:
No migrations to apply.
I don't know what is wrong with your account app . but you can solve it with :
1- delete __pycache__ and migrations folders located inside account app
2 - run python manage.py makemigrations account zero
3 - then run python manage.py makemigrations account
4 - finally run python manage.py migrate
I solved the following error by just dropping the whole Database and deleting the py_cache and migration files of my application. Just create another database and rerun the migration and migrate commands. Hope this helps.

Django create a new column in an already existing table

I'm using Django 3.0.5 and I am trying to create a new column in a table.
The table looks like this:
class VacationModel(models.Model):
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
emp_id = models.IntegerField()
email = models.EmailField()
from_date = models.DateField()
to_date = models.DateField()
reason = models.TextField()
time_sent = models.DateTimeField("date sent")
req_approved = models.BooleanField(default=False, null=True)
req_denied = models.BooleanField(default=False, null=True)
# daysoff_given = models.IntegerField()
def __str__(self):
return self.emp_id
The new column would be daysoff_given. I tried adding this column and after running python manage.py makemigrations I got an error saying django.db.utils.OperationalError: no such column
I tried following some other answers and I deleted the migrations made inside the migrations folder, without deleting the __init__.py file. After running makemigrations again the same error occured and then I deleted the whole model and made a new model.
I think my database is broken, but is there an actual way to avoid this, since it has already happened two times.
Whenever I try to add a new column, it always throws that error and I cannot continue. How can I fix this?
I think the problem is that you created migrations but didn't apply them. Make sure you run both of the following commands after adding the column in the Model.
python manage.py makemigrations
python manage.py migrate
It it doesn't work, please edit your question and add the full trackback error to help us know what is the causing the error.

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.

How to update Django model max_length value in database after tables have already been migrated

I need more characters available for the title and subtitle fields of a blog I made. I would like to increase the max_length from 100 to 150. Here is the table:
class Post(models.Model):
title = models.CharField(max_length=100)
subtitle = models.CharField(max_length=100)
slug = models.SlugField(max_length=99)
date_added = models.DateTimeField(default=timezone.now)
author = models.CharField(max_length=60)
body = models.TextField()
category = models.ForeignKey(Category, on_delete=models.CASCADE)
tags = models.ManyToManyField(Tag)
Through another Q&A I took the advice to change the max_length in the model (in my case from 100 to 150) and type this in the command prompt:
python manage.py makemigrations
python manage.py migrate
I then committed the changes and it allowed me to type more characters in but when I submitted the post it came up with a database error saying the fields can only take 100 characters.
How can I get the database to recognize the change in max_characters?
You can change it and re run the migrations again or do python manage.py migrate my_app 0008_previous_migration you can then delete the newer migration file with the error in it and re run the commands.
You can do python manage.py showmigrations my_app

django.db.utils.ProgrammingError: column "questions" is of type character varying[] but default expression is of type integer

This is my model:
class college(models.Model):
image = models.ImageField(upload_to='college_image/',default=None)
name = models.CharField(max_length=100)
email = models.CharField(max_length=100,unique=True)
password = models.CharField(max_length=500)
add_field = models.CharField(max_length=200)
city = models.CharField(max_length=200)
state = models.CharField(max_length=200)
zipcode = models.CharField(max_length=10)
phone = models.CharField(max_length=10, validators=[RegexValidator(r'^[789]\d{9}$')])
status = models.BooleanField(default = True)
last_date = models.DateTimeField()
is_active = models.BooleanField(default = False)
reg_time = models.DateTimeField(auto_now_add=True,blank=True)
questions = ArrayField(models.CharField(max_length=500),blank=True)
def __str__(self):
return self.email
An error occurred when I added a questions field in the model and used command 'python manage.py migrate', and the following hint was shown:
HINT: You will need to rewrite or cast the expression
What does it mean? What should I do?
I know this is an old question but hopefully this answer can help somebody else. I fixed this same error by doing the following:
Add default list to the ArrayField. E.g.
answers = ArrayField(models.IntegerField(default=list))
Delete migration files in migrations folder except __init__.py
python manage.py make migrations
python manage.py migrate
So the issue was that in one of the migrations, the field was trying to be set to default 0 (programming error) but even after I fixed it in the code, the old migration was affected and thus it kept failing.

Categories

Resources