Django ProgrammingError Caused By Model Field - python

I have a model called "news", defined below:
class News(models.Model):
title = models.CharField(max_length=30, null=False, blank=False, verbose_name="news title")
content = models.TextField(max_length=300, null=False, blank=False, verbose_name="news content")
cta = models.CharField(max_length=50, null=False, blank=False, verbose_name="news call-to-action")
mini_image = models.URLField(null=False, blank=False, verbose_name="news image helper")
is_promo = models.BooleanField(null=False, blank=False, verbose_name="promo code")
promo_benefit = models.DecimalField(max_digits=7, decimal_places=2, blank=False, null=False, default=0.00, verbose_name="promo benefit")
promo_duration = models.IntegerField(null=False, blank=False, default=0, verbose_name="promo duration")
date_published = models.DateTimeField(auto_now_add=True, null=False, blank=False)
def __str__(self):
return self.title
And when I try to access a template that uses the news model (whether I'm logged into admin trying to create a new instance or on a custom template), I get the following error:
ProgrammingError at /admin/myapp/news/
column omninectar_news.cta does not exist
LINE 1: ...app_news"."title", "myapp_news"."content", "myapp...
^
Any ideas on how I can fix this issue?

column omninectar_news.cta does not exist
That should be self-explanatory. Your database is out of date.
If you use South, migrate. Otherwise, try to remove the table "omnictar_news" and then run syncdb.

You should to delete the migrations folder and then
python manage.py migrate --run-syncdb
python manage.py migrate --fake appname

Related

django user_groups table missing

I ahve a user model called TbUser and I have integrated a mysql legacy database with django. After doing migrations I have the follwing tables. django_seesion, django_migrations, django_content_type, django_admin_log, auth_permission, auth_group_permissions, auth_group
When I log in to django admin page, and click on the TbUser then select a random user I am getting the following error.
Exception Value:
(1146, "Table 'db.tb_user_groups' doesn't exist")
Should this table be created when migrations are run?
Could it be that this table is the auth_group, and django is looking for this one using the wrong table name?
users.models
class TbUser(AbstractBaseUser, PermissionsMixin):
id = models.CharField(primary_key=True, max_length=32, default=uuid.uuid4)
username = models.CharField(
max_length=40, blank=True, null=True, unique=True, db_column='usname')
password = models.CharField(
max_length=255, blank=True, null=True, db_column='psword')
email = models.CharField(max_length=255, blank=True, null=True)
role = models.ForeignKey(TbRole, on_delete=models.CASCADE)
department = models.ForeignKey(
'app.TbDepartment', on_delete=models.CASCADE, null=True, blank=True)
is_superuser = models.BooleanField(
default=False, blank=True, null=True, db_column='default_super')
is_staff = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
objects = TbUserManager()
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['email']
class Meta:
managed = False
db_table = 'tb_user'
admin.py
from app.models import TbDepartment
from django.contrib import admin
from .models import TbCustomer, TbRole, TbUser, TbUserRole
admin.site.register(TbUser)
admin.site.register(TbRole)
admin.site.register(TbUserRole)
admin.site.register(TbDepartment)
admin.site.register(TbCustomer)
UPDATE:
I have created in mysql a table called tb_user_groups and when doing the same action the next error appears
Exception Value:
(1146, "Table 'db.tb_user_user_permissions' doesn't exist")
How do I generate these tables?
Why don't you just use AbstractUser. It has everything configured, and you can customize it if you want. In this case you shouldn't get tb_user_groups doesn't exist error.
class TbUser(AbstractUser):
REQUIRED_FIELDS = []
objects = TbUserManager()

Not Displaying error message in Django Admin?

Whenever i try to add project of same name it leads to IntegrityError at /admin instead there should be an Error message on Django admin that "This project already exists". Also i have two users. So same user cannot have project of same name and different users can have project of same name. Please let me know how to fix this.
class Project(models.Model):
name = models.CharField(max_length=200)
added_by = models.ForeignKey(
settings.AUTH_USER_MODEL, on_delete=models.CASCADE, blank=True, null=True, default=None)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
unique_together = (("name", "added_by"),)

Migrations being applied according to User in models.py instead of being applied according to migrations in the database

I have written a custom django migrations command as shown below
User = get_user_model()
def populate_asset_assignee(apps, schema_editor):
for user in User.objects.all():
user.save()
# Some more code
The user model looks as follows
class User(AbstractUser):
username = None
email = models.EmailField(max_length=50, unique=True)
cohort = models.IntegerField(blank=True, null=True)
slack_handle = models.CharField(max_length=50,
blank=True, null=True)
picture = models.CharField(max_length=255, blank=True, null=True)
phone_number = models.CharField(max_length=50, blank=True, null=True)
last_modified = models.DateTimeField(auto_now=True, editable=False)
password = models.CharField(max_length=128, blank=True, null=True)
location = models.ForeignKey('SomeCentre',
blank=False,
null=True,
on_delete=models.PROTECT)
# Some more fields
I added the location field recently and have the migrations for it which is applied after this custom migration has been applied. The problem I am having is that whenever I try to make the migrations on the test db or a new database, I get the error django.db.utils.ProgrammingError: column core_user.location_id does not exist which is being raised inside the populate_asset_assignee method when I try to do the user in User.objects.all()
Any ideas why location_id is beeing checked yet I haven't applied the migrations for the location field yet.

Python Django: Remove null=true from Model CharField

Unfortunately I have a large model in production (Django 2.0.5) defined with null=True on CharFields:
class FooterMenu(models.Model):
text_de = models.CharField(verbose_name=u"Menüpunkt (de)", max_length=100, default='', blank=True, null=True)
text_en = models.CharField(verbose_name=u"Menüpunkt (en)", max_length=100, default='', blank=True, null=True)
text_fr = models.CharField(verbose_name=u"Menüpunkt (fr)", max_length=100, default='', blank=True, null=True)
Unfortunately there are plenty of NULL Values in the postgres DB already.
If I just remove the null=True and makemigrations the migrate tells me: cannot ALTER TABLE "doctor_footermenu" because it has pending trigger events
I understand this happens because of the NULL values in the table.
Is there a proven strategy that I can use?

migrating an existing app via south

I have the following model -
class ToDo(models.Model):
todo_title = models.CharField(null=True, blank=True, max_length=200)
todo_status = models.IntegerField(choices=TASK_STATUS, null=True, blank=True)
assigned_to = models.ManyToManyField(OrgStaff, null=True, blank=True, related_name='assigned_to')
assigned_by = models.ManyToManyField(OrgStaff, null=True, blank=True, related_name='assigned_by')
assigned_time = models.DateTimeField(auto_now_add=True)
completed_time = models.DateTimeField(null=True, blank=True)
I then run python manage.py convert_to_south todoapp where todoapp is the name of the
app. Then I run python manage.py migrate todoapp.
Once that is done, I add another field in the above model -
class ToDo(models.Model):
todo_title = models.CharField(null=True, blank=True, max_length=200)
todo_slug = models.SlugField(null=True, blank=True)
todo_status = models.IntegerField(choices=TASK_STATUS, null=True, blank=True)
assigned_to = models.ManyToManyField(OrgStaff, null=True, blank=True, related_name='assigned_to')
assigned_by = models.ManyToManyField(OrgStaff, null=True, blank=True, related_name='assigned_by')
assigned_time = models.DateTimeField(auto_now_add=True)
completed_time = models.DateTimeField(null=True, blank=True)
Now I do a schemamigation - python manage.py schemamigration todoapp --auto and then python manage.py migrate todoapp doing this gives the following error -
Running migrations for taskbase:
- Migrating forwards to 0002_auto__add_field_todo_todo_slug.
> taskbase:0002_auto__add_field_todo_todo_slug
KeyError: u'todo_title'
Any idea why I am getting this error?
I banged my head, but unable to find the reason.
It's possible that prefixing 'title' and 'status' with 'todo_' is causing a collision with the name of the table fields. In fact, in the database Django is naming the fields todoapp_todo_todo_status, and South might just be confused. South does some creative things internally, hence the collision. I would suggest trying:
class ToDo(models.Model):
title = models.CharField(null=True, blank=True, max_length=200)
status = models.IntegerField(choices=TASK_STATUS, null=True, blank=True)
assigned_to = models.ManyToManyField(OrgStaff, null=True, blank=True, related_name='assigned_to')
assigned_by = models.ManyToManyField(OrgStaff, null=True, blank=True, related_name='assigned_by')
assigned_time = models.DateTimeField(auto_now_add=True)
completed_time = models.DateTimeField(null=True, blank=True)
I wanted to get pedantic, I could also point out that todoapp should be called todos, but that won't a difference to your project.

Categories

Resources