django.db.utils.IntegrityError in Custom UserModel - python

Custom User Model:
class User(AbstractBaseUser):
id = models.AutoField(primary_key=True)
username = models.CharField(max_length=30)
email = models.EmailField(unique=True,max_length=75)
test = models.CharField(max_length=20)
USERNAME_FIELD = 'email'
class Meta:
managed = False
db_table = 'auth_user'
On running ./manage.py migrate, I get the following error:
django.db.utils.IntegrityError: (1215, 'Cannot add foreign key constraint')
SHOW ENGINE INNODB STATUS on mysql shows:
2015-07-12 19:29:25 133f4a000 Error in foreign key constraint of table edutraffic/#sql-55ea_17a:
FOREIGN KEY (`user_id`) REFERENCES `auth_user` (`id`):
Cannot resolve table name close to:
(`id`)
So, I deleted the complete database and recreated it, still getting the same error.
settings.py file:
AUTH_USER_MODEL = 'users.User'
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.staticfiles',
'django.contrib.sites',
'rest_framework',
'rest_framework.authtoken',
'rest_auth',
'allauth',
'allauth.account',
'rest_auth.registration',
'allauth.socialaccount',
'allauth.socialaccount.providers.facebook',
'allauth.socialaccount.providers.google',
'users',
)

You have set managed to False, which means that Django will not create the table. Either remove that line, or create the table manually.

Related

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.

Django Admin override UserAmin

I used this admin code, to adjust my
class CustomUserAdmin(UserAdmin):
list_display = UserAdmin.list_display + ('show_url', 'date_joined')
def show_url(self, obj):
return format_html("<a href='{url}'>Link</a>", url=obj.extendeduser.get_link())
admin.site.unregister(User)
admin.site.register(User, CustomUserAdmin)
But the admin just seems to ignore this and displays the old admin page. What could i have missed?
The code is placed in an admin.py of an app I called "main", which is listed in the INSTALLED_APPS list:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sitemaps',
'main',
...
]
To create custom user admin pages easier, see: django-authtools.

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!

NameError: name 'FileAttachment' is not defined

I have two different Django Apps, one of them is fileUpload. In File Upload I added a Generic Relation as:
from django.db import models
from django.contrib.contenttypes import generic
from django.contrib.contenttypes.models import ContentType
class FileAttachment(models.Model):
file = models.FileField(upload_to='fileuploads/%Y-%m-%d/')
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
attachee = generic.GenericForeignKey()
In my other app named XYZ I added following reverse generic relation in models.py
attachment = generic.GenericRelation(FileAttachment)
Now, so if I run manage.py syncdb or any other manage command, I get error: NameError: FileAttachment
IN installed Apps I have following stuff:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'django.contrib.markup',
'django.contrib.humanize',
'south',
'taggit',
'taggit_autocomplete_modified',
'compressor',
'bootstrapform',
'fileupload',
'XYZ'
)
Both apps, XYZ and fileupload are on same root level.
I am using Django 1.5 with Python2.7
Did you import the FileAttachement in the XYZ models?
Try this in XYZ/models.py:
from fileupload.models import FileAttachment

Django Can't Find Installed App

I have my directory structure like this for my Django application:
project
__init__.py
settings.py
...
users
__init__.py
model.py
....
In my model.py I have this class:
class User():
...
However when I configure my INSTALLED_APPS like this:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'south',
'users'
# Uncomment the next line to enable the admin:
# 'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
)
Django cannot find the application users. What am I doing wrong?
Change model.py to models.py, or if you want to use a different name for your module append the app_label argument to your models Meta classes:
class YourModel(models.Model):
# fields...
class Meta:
app_label = 'users'

Categories

Resources