The command migration did not execute - python

I have a challenge here. I am currently building an application,and I tried to add my app to the settings,in this way :
INSTALLED_APPS = [
'polls.apps.PollsConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
In the model,here is the code :
from django.db import models
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
Initially I used this command in the terminal
cd myproject
manage.py migratrate
Here is the output: Apply all migrations: admin,auth,contenttypes,sessions
Running
migrations:No migrations to apply.
Having defined my database in the model,I used the following command.Please,how do I fix this?
Note that I am a beginner in Python and Django

Please check that the polls/migrations/__init__.py file and directory exists. If the polls app is registered correctly in INSTALLED_APPS and the model is registered correctly but the migration file is not created, the next thing to check is to check if the migration directory is normal. Django checks if the __init__.py file exists in the migrations folder, so if it doesn't exist, it won't generate a migration file.
the polls app without polls/migrations/__init__.py:
the polls app with polls/migrations/__init__.py:

Related

Migrations are not applying to specific model

The problem is whenever I try to migrate changes, migrations does'nt applying to this specific app which is named as Userinfo. The messege in my terminal is
Operations to perform:
Apply all migrations: admin, auth, books_details, contenttypes, sessions
Running migrations:
No migrations to apply.
And that app is not listed in my above list also i don't know what could be the reason
Models.py of that app
from django.db import models
import os
# from django.conf import settings
def upload_rename(instance,filename):
exe=filename.split('.')[-1]
filename=instance.user_name+'.'+exe
try:
os.remove(os.path.join('images','profile_image',instance.user_name,filename))
except:
pass
return os.path.join('profile_image',instance.user_name,filename)
class Userinfo(models.Model):
''' User info '''
user_name = models.CharField(max_length=30, unique=True, null=False,primary_key=True)
full_name = models.CharField(max_length=50, null=False)
user_email = models.EmailField(max_length=254)
college_name = models.CharField(max_length=50)
city = models.CharField(max_length=50)
country = models.CharField(max_length=50)
profile_img = models.ImageField(upload_to=upload_rename, blank=True)
''' The Change I added '''
varified_user =models.BooleanField(default=False)
Admin.py of that app
from django.contrib import admin
from .models import Userinfo
admin.site.register(Userinfo)
INSTALLED_APP in setting.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'userinfo',
'rest_framework',
'phonenumber_field',
'books_details',
]
Migrations
from django.db import migrations, models
import userinfo.models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Userinfo',
fields=[
('user_name', models.CharField(max_length=30, primary_key=True, serialize=False, unique=True)),
('full_name', models.CharField(max_length=50)),
('user_email', models.EmailField(max_length=254)),
('college_name', models.CharField(max_length=50)),
('city', models.CharField(max_length=50)),
('country', models.CharField(max_length=50)),
('profile_img', models.ImageField(blank=True, upload_to=userinfo.models.upload_rename)),
('varified_user', models.BooleanField(default=False)),
],
),
]
Here are some images i think you should see
I don't know why it says create model userinfo as it was pre-existing
Database screenshot
please any can help with this???
i guess you need first to run makemigrations command:
python manage.py makemigrations userinfo
and then run migrate command:
python manage.py migrate
refer to this tuto from django docx :https://docs.djangoproject.com/en/3.1/intro/tutorial02/#activating-models for more explanations
By running makemigrations, you’re telling Django that you’ve made some changes to your models (in this case, you’ve made new ones) and that you’d like the changes to be stored as a migration.
Update
in some cases, it's better to restart the migration process from scratch just to unlock such situation
so, since you're in development mode i would suggest you deleting:
the database (or better make a backup)
migrations files under migrations folder for each app
and don't forget __pycache__ folders
and then rerun makemigrations and migrate commands respectively

Django 3 OperationalError: no such table 'web_user'

I want to have 2 types of users in my Django app, so I followed this tutorial and ran
python manage.py makemigrations web
python manage.py migrate
('web' is the name of my app)
Now I want to access the admin part of the site, automatically added by django at localhost:PORT/admin. When I try to access that page, this error shows:
django.db.utils.OperationalError: no such table: web_user
Here's my models.py:
from django.contrib.auth.models import AbstractUser
from django.db import models
from web import constants
class User(AbstractUser):
USER_TYPE_CHOICES = (
(constants.USER_TYPE_CLEANER, 'cleaner'),
(constants.USER_TYPE_CONTRACTOR, 'contractor'),
)
user_type = models.PositiveSmallIntegerField(choices=USER_TYPE_CHOICES)
# extra fields
email = models.CharField(max_length=100)
phone_number = models.CharField(max_length=15)
date_of_birth = models.DateField('date_of_birth')
address = models.CharField(max_length=50)
city = models.CharField(max_length=25)
state = models.CharField(max_length=25)
and set this in settings.py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'web.apps.WebConfig',
]
# Authentication
AUTH_USER_MODEL = 'web.User'
How can I enable django's admin site? It's very useful for creating demo data.
make sure that before running makemigrations and migrate, add your app name in INSTALLED_APPS in project settings
Thanks to #bmons , the answer was to delete database db.sqlite3, migration files, and create a new user. Note that the extra fields must be nullable, otherwise creating a superuser will fail.

How to make a model show up in the admin-section, in Django?

I am new to Django and Python.
Currently, I am creating a website for weightlifting. Below is a simple model, with a lifter and results:
class Lifter(models.Model):
name = models.CharField(max_length=250)
gender = models.CharField(max_length=1);
person_photo = models.CharField(max_length=1000);
def __str__(self):
return self.name + ' - ' + self.gender
class Results(models.Model):
lifter = models.ForeignKey(Lifter, on_delete=models.CASCADE)
points = models.CharField(max_length=4, default=0);
In the same app, there is a file called admin.py where I wrote:
from django.contrib import admin
from .models import Lifter
admin.site.register(Lifter)
Unfortunately, when I log into the admin-panel, there is no lifter-model to be seen. I have made sure to create the admin as a super-user, so the permissions should be there. In addition to this, the "usermanagement"-app (where the Lifter-model is stored) , has been added to settings.py in the main app:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'athlitikos',
'usermanagement',
]
Does anyone know what I should change to make this work?
Thank you!
your code looks fine to me
try to unregister and then again register in admin.py

Migration fails when extending Django User Model

I'm trying to extend django User model by inheriting AbstractBaseUser so i can be able to manipulate the authentication process of the project.
Here is what my model looks like.
class AccountManager(BaseUserManager):
... create_user
... create_superuser
class Account(AbstractBaseUser):
email = models.EmailField(unique=True)
username = models.CharField(max_length=40, unique=True)
objects = AccountManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username']
And here is my settings INSTALLED_APPS
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'debug_toolbar',
'rest_framework',
'compressor',
'authentication'
]
AUTH_USER_MODEL = 'authentication.Account'
The problem here i notice the migration process that django is bypassing the auth.0001_initial and it jumped directly creating the admin.0001_initial making my migrations to fail with
django.db.utils.IntegrityError: (1215, u'Cannot add foreign key constraint')
How can i fixed this please help?
I was able to solve my issue, by this simple steps:
run python manage.py makemigrations authentication - because when using AUTH_USER_MODEL it will replace the migration of auth_user table of django.contrib.auth altering the migration process. Thus if we fail to provide migration file for authentication app migration will no doubt fails.
run python manage.py migrate.
Binggo!

Django Python __init__() TypeError 'Default' Tutorial

I'm going through the Django Tutorial: https://docs.djangoproject.com/en/1.5/intro/tutorial01/
I have an error:
TypeError: __init__() got an unexpected keyword argument 'Default'
This occurs when I call:
$python manage.py sql polls
OR
$python manage.py syncdb
I'm stuck and can't figure out what I'm doing wrong.
I just started the "Activating models" section of the tutorial and input the installed applications
TrackBack:
[settings.py]
'Engine': 'django.db.backends.sqlite3'
'NAME': '/Users/msmith/Documents/djangoPractice/database.db'
$ python manage.py syncdb
$ python manage.py startapp polls
[polls.models.py]
from django.db import models
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(Default=0)
[settings.py] - Added 'polls' to the bottom of the list
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
# 'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
'polls',
)
$python manage.py sql polls -> Error Occurs
In your models definitions, you have Default= instead of default=. Check the case.
class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
# ^ Check the case here

Categories

Resources