So I added the following line in my settings.py:
AUTH_USER_MODEL = 'app.CustomUser'
And I got the error:
LookupError: App 'app' doesn't have a 'CustomUser' model.
The last line in error is:
django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model 'app.CustomUser' that has not been installed
Here is my CustomerUser class in app/models.py:
from django.db import models
from django.contrib.auth.models import User
from django.contrib.auth.models import AbstractUser
from django.utils import timezone
class CustomUser(AbstractUser):
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='CustomUser')
name = models.CharField(max_length=500)
phone = models.CharField(max_length=30)
email = models.CharField(max_length=500)
class Meta:
abstract = True
I get this error while trying to run makemigrations. I already have an existing db, and I'm redoing my models. I tried deleting the database file, and no luck.
Related
I'm getting this error.
ERRORS: subscriptions.StripeCustomer.user: (fields.E301) Field defines
a relation with the model 'auth.User', which has been swapped out.
HINT: Update the relation to point at 'settings.AUTH_USER_MODEL'.
I'm trying to configure Django Stripe Subscriptions following this manual https://testdriven.io/blog/django-stripe-subscriptions/
My models.py
from django.contrib.auth.models import User
from django.db import models
class StripeCustomer(models.Model):
user = models.OneToOneField(to=User, on_delete=models.CASCADE)
stripeCustomerId = models.CharField(max_length=255)
stripeSubscriptionId = models.CharField(max_length=255)
def __str__(self):
return self.user.username
My admin.py
from django.contrib import admin
from subscriptions.models import StripeCustomer
admin.site.register(StripeCustomer)
My settings.py
#used for django-allauth
AUTH_USER_MODEL = 'accounts.CustomUser'
DEFAULT_AUTO_FIELD='django.db.models.AutoField'
SITE_ID = 1
AUTHENTICATION_BACKENDS = (
'allauth.account.auth_backends.AuthenticationBackend',
'django.contrib.auth.backends.ModelBackend',
)
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
ACCOUNT_EMAIL_VERIFICATION = "none"
accounts/models.py
from django.contrib.auth.models import AbstractUser
class CustomUser(AbstractUser):
class Meta:
verbose_name_plural = 'CustomUser'
After setting above, I executed "python manage.py makemigrations && python manage.py migrate" then the error occurred.
I just mentioned the above settings in this question but still if more code is required then tell me I'll update my question with that information. Thank you
You have your OneToOneField pointing to the User model from django.contrib.auth when in fact you are using a custom user model CustomUser, hence you get the error. Generally if one wants to have a foreign key or any related field with the user model one should point it to settings.AUTH_USER_MODEL as described in the Referencing the User model [Django docs] so that such issues can be prevented easily. Hence change your StripeCustomer model like so:
from django.conf import settings
from django.db import models
class StripeCustomer(models.Model):
user = models.OneToOneField(to=settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
stripeCustomerId = models.CharField(max_length=255)
stripeSubscriptionId = models.CharField(max_length=255)
def __str__(self):
return self.user.username
This keep popping up on web after save an object, not sure what happens: screenshot
OperationalError at /admin/product/product/add/
no such table: product_product
Request Method: POST
Request URL: http://127.0.0.1:8000/admin/product/product/add/
Django Version: 3.1.2
Exception Type: OperationalError
Exception Value:
no such table: product_product
My code:
admin.py
from django.contrib import admin
# Register your models here.
from .models import product
admin.site.register(product)
models.py
from django.db import models
# Create your models here.
class product(models.Model):
title = models.CharField(max_length=222)
description = models.TextField(blank=True, null=True)
price = models.DecimalField(max_digits=333, decimal_places=2)
summary = models.TextField(default = 'this is cool!')
feature = models.BooleanField()
apps.py
from django.apps import AppConfig
class ProductConfig(AppConfig):
name = 'product'
I found out it's migration problem, if it states No changes detected, it doesn't mean no change needed to be made, as what I missed is the app file name typed after makemigrations in terminal.
I am trying to customize djoser's createuser end point. For that I have installed django custom user following this link https://github.com/jcugat/django-custom-user. Here is my models.py
from django.db import models
from custom_user.models import AbstractBaseUser
class Profile(AbstractBaseUser):
account_address = models.CharField(max_length=30, blank=True)
and serializers.py
from djoser.serializers import UserCreateSerializer as BaseUserRegistrationSerializer
class UserRegistrationSerializer(BaseUserRegistrationSerializer):
class Meta(BaseUserRegistrationSerializer.Meta):
fields = ('url', 'id', 'email', 'first_name', 'account_address', 'password')
and in app.admin.py i have registered it in following way.
from django.contrib import admin
from custom_user.admin import UserAdmin
from .models import Profile
class MyCustomEmailUserAdmin(UserAdmin):
"""
You can customize the interface of your model here.
"""
pass
# Register your models here.
admin.site.register(Profile, UserAdmin)
but when I am trying to makemigrations i am running into following error.
any clue what's wrong here?
From the looks of it, you seem to have the following in your settings:
AUTH_USER_MODEL = "your_app.Profile"
And for your profile model, you are inheriting from AbstractBaseUser which is actually from from django.contrib.auth.models import AbstractBaseUser.
I believe you meant to inherit from AbstractEmailUser. Thus, your "Profile" model would actually need be:
from custom_user.models import AbstractEmailUser
class Profile(AbstractEmailUser):
account_address = models.CharField(max_length=30, blank=True)
I have these models below
# user profile models file
from ad.models import FavoriteAd
class UserProfile(models.Model):
def get_user_favorite_ad(self):
return FavoriteAd.objects.filter(fav_user=self)
# ad models file
from user_profile.models import UserProfile
class FavoriteAd(models.Model):
fav_user = models.ForeignKey(UserProfile, blank=False, on_delete=models.CASCADE)
I have tried using these but it give me the NameError UserProfile not found
# ad models files
class FavoriteAd(models.Model):
fav_user = models.ForeignKey('user_profile.UserProfile', blank=False, on_delete=models.CASCADE)
Also tried these as well still got error that model are not ready
# ad models files
from django.apps import apps
UserProfile = apps.get_model('user_profile', 'UserProfile')
class FavoriteAd(models.Model):
fav_user = models.ForeignKey(UserProfile, blank=False, on_delete=models.CASCADE)
You are using FavoriteAd inside get_user_favorite_ad method of
UserProfile model
Thats the reason you are unable to import it in FavoriteAd and this is causing circular import.
For fetching favorite ads of that user, Use favoritead_set to get related objects
# remove that import as well
# from ad.models import FavoriteAd
class UserProfile(models.Model):
def get_user_favorite_ad(self):
return self.favoritead_set.all()
I have made a custom profile model which looks like this:
from django.db import models
from django.contrib.auth.models import User
class UserProfile(models.Model):
user = models.ForeignKey('User', unique=True)
name = models.CharField(max_length=30)
occupation = models.CharField(max_length=50)
city = models.CharField(max_length=30)
province = models.CharField(max_length=50)
sex = models.CharField(max_length=1)
But when I run manage.py syncdb, I get:
myapp.userprofile: 'user' has a relation with model User, which has
either not been installed or is abstract.
I also tried:
from django.contrib.auth.models import BaseUserManager, AbstractUser
But it gives the same error. Where I'm wrong and how to fix this?
Exactly in Django 1.5 the AUTH_USER_MODEL setting was introduced, allowing using a custom user model with auth system.
If you're writing an app that's intended to work with projects on Django 1.5 through 1.10 and later, this is the proper way to reference user model (which can now be different from django.contrib.auth.models.User):
class UserProfile(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL)
See docs for more details.
In case you're writing a reusable app supporting Django 1.4 as well, then you should probably determine what reference to use by checking Django version, perhaps like this:
import django
from django.conf import settings
from django.db import models
def get_user_model_fk_ref():
if django.VERSION[:2] >= (1, 5):
return settings.AUTH_USER_MODEL
else:
return 'auth.User'
class UserProfile(models.Model):
user = models.ForeignKey(get_user_model_fk_ref())
Change this:
user = models.ForeignKey('User', unique=True)
to this:
user = models.ForeignKey(User, unique=True)