Trying to add a DateTimeField in the admin panel for articles
Exception Type: OperationalError
Exception Value: no such column: blog_article.date
Admin.py
from django.contrib import admin
from blog.models import Article
class ArticleAdmin(admin.ModelAdmin):
list_display = ('title', 'date')
admin.site.register(Article, ArticleAdmin)
Models.py
from django.contrib.auth.models import User
from django.db import models
class Article(models.Model):
title = models.CharField(max_length=200)
text = models.TextField("Text")
date = models.DateField()
user = models.ForeignKey(User)
Try this command in Terminal:
python manage.py makemigrations
python manage.py migrate
Alternative:
Exclude your migrations located in the folder /migrations/ and make migrations again.
Related
from django.db import models
from django.contrib.auth import get_user_model
user = get_user_model
# Create your models here.
class Post(models.Model):
title = models.CharField(max_length=200)
text = models.TextField(max_length=100)
author = models.ForeignKey(
user,
on_delete=models.CASCADE)
created_date = models.DateTimeField()
published_date = models.DateTimeField()
When I run:
python manage.py migrate
Then shows:
PS C:\Users\hp\Desktop\djangomodels\I4G006918VEO> python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
No migrations to apply.
It should be user=get_user_model() not user=get_user_model.
It returns the User model that is active in this project.
Try this:
from django.db import models
from django.contrib.auth import get_user_model
user = get_user_model()
# Create your models here.
class Post(models.Model):
title = models.CharField(max_length=200)
text = models.TextField(max_length=100)
author = models.ForeignKey(
user,
on_delete=models.CASCADE)
created_date = models.DateTimeField()
published_date = models.DateTimeField()
Then, run both the commands as python manage.py makemigrations and python manage.py migrate.
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
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.
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.
How fix this? This after when i create new post and when i press button 'add'
It looks like your post is mostly code; please add some more details. Why stackoverflow ask this?
#models.py
from django.db import models
from django.contrib.auth.models import User
class Post(models.Model):
title = models.CharField(max_length=200)
slug = models.SlugField(max_length=255)
author = models.ForeignKey(User, on_delete= models.CASCADE,related_name='blog_posts')
content = models.TextField()
created_on = models.DateTimeField(auto_now_add=True)
updated_on = models.DateTimeField(auto_now= True)
def __str__(self):
return self.title
#admin.py
from django.contrib import admin
from .models import Post
class PostAdmin(admin.ModelAdmin):
prepopulated_fields = {'slug': ('title',), }
admin.site.register(Post,PostAdmin)
Maybe you forgot to use management commands, makemigrations and migrate after removing status field from your Post model. As far as I can see you don't have status field in your model but this field is still exists in your database. So make sure that field is properly removed from your database by using these management commands (makemigrations and migrate) or if your data is not important you can do the following procedure:
a- Drop your database and create new one
b- Remove all migration files
c- Run manage.py makemigrations and manage.py migrate