Python Migration - python

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.

Related

How to assign a database model to a user

from django.db import models
from datetime import datetime
from django.contrib.auth.models import User
class News(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
headline = models.CharField(max_length=100)
content = models.CharField(max_length=1000000)
time_created = models.DateTimeField(default=datetime.now, blank=True)
i kept on trying this method but i keep getting an error" django.db.utils.OperationalError: no such column: blog_news.user_id "
I am trying to make a logged-in user view only his/her contributions
Did you use "python manage.py migrate" command? (and of course, after that "python manage.py makemigrations" command)
Probably, you didn't run these commands and the database can't find such a column.

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.

How can I handle the "Django - Database error: no such table" error?

I'm pretty new to django and I'm stuck at the problem with models. Here is my users app's models file:
from django.db import models
from django.contrib.auth.models import User
from django.core.validators import MinValueValidator, MaxValueValidator
from django.urls import reverse
from PIL import Image
class Schedule(models.Model):
title = models.CharField(max_length=150)
context = models.TextField()
class Input(models.Model):
DAYS_OF_FITNESS = [
('month', '30'),
('month_2', '60'),
('month_3', '90'),
]
weight = models.PositiveIntegerField(validators=[MinValueValidator(40)])
age = models.PositiveIntegerField(validators=[MinValueValidator(12)])
days = models.CharField(
max_length=20, choices=DAYS_OF_FITNESS, default='30')
athlete = models.OneToOneField(User, primary_key=True, on_delete=models.CASCADE)
schedule = models.ForeignKey(Schedule, on_delete=models.CASCADE)
I'm trying to link each user with its input using OneToOneField and Schedule is linked as a foreign key to Input model
But when I'm trying to migrate models, I'm getting this error:
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: users_input
I checked that Both models are in django settings, and I migrated them. BTW, I'm using signals to save inputs automatically, (just for note if it helps)
It's because that table doesn't exist in the database. To create tables you have to do the following. Execute the following commands in your terminal.
python manage.py makemigrations
python manage.py migrate

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