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.
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
I'll keep this short because I haven't had great luck with posts in the past. I'm building a website that uses the quill text editor (unimportant) and saves the text to a database which can later be used to edit. Everything runs perfectly in my local hosted server when I run "python manage.py runserver". However when I run it on Heroku to host it online I get this error...
Request Method: GET
Request URL: https://vast-river-12269.herokuapp.com/signup/new_search/posts/create/
Django Version: 3.1.2
Exception Type: AttributeError
Exception Value:
'NoneType' object has no attribute 'json_string'
Exception Location: /app/.heroku/python/lib/python3.6/site-packages/django_quill/forms.py, line 17,
in
prepare_value
Python Executable: /app/.heroku/python/bin/python
Python Version: 3.6.13
Python Path:
['/app/.heroku/python/bin',
'/app',
'/app/.heroku/python/lib/python36.zip',
'/app/.heroku/python/lib/python3.6',
'/app/.heroku/python/lib/python3.6/lib-dynload',
'/app/.heroku/python/lib/python3.6/site-packages']
It says no attribute json_string in NoneType which seems very generic. I also haven't worked with json in my project and I don't see it anywhere. I have however narrowed the problem down to one line of code. In my HTML I have a line calling a variable "{{ forms.content }}. This line is the problem and I t correlates to the bellow models.py file and forms.py file. They are attached below but I cant find any problem in there and it works on my local host so I don't understand.
What I want to know in this post.
How to fix this problem
Why this problem happened
models.py...
from django.db import models
from django.contrib.auth.models import AbstractUser, UserManager
from django.conf import settings
User = settings.AUTH_USER_MODEL
from django.conf import settings
from django.urls import reverse
from django_quill.fields import QuillField
# Create your models here.
class QuillPost(models.Model):
content = QuillField()
class Meta:
ordering = ['-pk']
def get_absolute_url(self):
return reverse('quill-post-detail', args=[self.pk])
class Text(models.Model):
document = models.CharField(max_length=100)
def __str__(self):
return self.document
class CustomUserManager(UserManager):
pass
class CustomUser(AbstractUser):
objects = CustomUserManager()
class Search(models.Model):
#user = models.ForeignKey(User, on_delete=models.CASCADE)
search = models.CharField(max_length=500)
created = models.DateTimeField(auto_now=True)
def __str__(self):
return '{}'.format(self.search)
class Meta:
verbose_name_plural = 'Searches'
forms.py...
from django import forms
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from .models import CustomUser
from .models import Text
from django_quill.forms import QuillFormField
from .models import QuillPost
class QuillForm(forms.Form):
quill = QuillFormField()
class QuillFieldForm(forms.Form):
content = QuillFormField()
def save(self):
return QuillPost.objects.create(content=self.cleaned_data['content'])
class QuillPostForm(forms.ModelForm):
class Meta:
model = QuillPost
fields = ['content']
sorry for the long post but I really need help.
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.
I have model registered on admin page:
models.py
from django.db import models
class Question(models.Model):
cat = models.IntegerField()
quest = models.TextField(max_length=200)
answer = models.CharField(max_length=1000)
path = models.FilePathField()
date = models.DateTimeField(auto_now_add=True)
status = models.IntegerField(default=0)
def __str__(self):
return f'{self.cat} - {self.quest}'
admin.py
from django.contrib import admin
from .models import Question
admin.site.register(Question)
and I can see a database through admin page:
https://i.stack.imgur.com/SuCcX.png
but I can't click on any record of the table and modify it due to an error:
https://i.stack.imgur.com/M6W5a.png
I did it many times since now, and I have never encountered such an error.
Does anybody have an idea how to fix it?
Acorrding to Django docs FilePathField() has one required argument path, which is:
"The absolute filesystem path to a directory from which this FilePathField should get its choices."
So you need modify your models.py:
class Question(models.Model):
...
path = models.FilePathField(path='/home/images')
...
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.