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.
Related
I am using Custom User model for only admins but I have to create seperate login sysytem for clients who uses django allauth google login system and these clients have below model:
class ClientUser(models.Model):
username = models.CharField(max_length=50, blank=True, null=True, unique=True)
email = models.EmailField(("email address"), unique=True)
first_name = models.CharField(max_length=50, blank=True, null=True, unique=True)
last_name = models.CharField(max_length=50, blank=True, null=True, unique=True)
phonenumber = models.CharField(max_length=15, null=True)
def __str__(self):
return "{}".format(self.email)
How could I make google allauth use this model?
Model:
class Comment(models.Model, CharField, ListField):
user = models.ForeignKey('auth.User', on_delete=models.CASCADE, related_name='comment_user',
blank=True, null=True)
news = models.ForeignKey(News, related_name='comment_of', on_delete=models.CASCADE)
content = models.CharField(validators=[MinLengthValidator(4)], max_length=200")
parent_comment = models.ForeignKey('self', blank=True, null=True, related_name='parent',
on_delete=models.CASCADE)
class Meta:
ordering = ['-created']
def __str__(self):
return self.content
When I login in admin click comment,there is an error:
Direct assignment to the reverse side of a related set is prohibited. Use parent.set() instead.
When I remove:
parent_comment = models.ForeignKey('self', blank=True, null=True, related_name='parent',
on_delete=models.CASCADE)
The error gone.
So,how to modify parent_comment,I think the issue is here.
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.
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?
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