django-migrations

How to add a permission to a user/group during a django migration?

浪子不回头ぞ 提交于 2019-12-03 14:11:09
I would like to execute the following migration: # -*- coding: utf-8 -*- from __future__ import unicode_literals from django.contrib.auth.models import Permission from django.db import migrations from django.conf import settings from django.contrib.auth.models import Group, User def add_api_group(apps, schema_editor): Group.objects.create(name=settings.API_USER_GROUP) # get_or_create returns a tuple, not a Group group = Group.objects.get(name=settings.API_USER_GROUP) permissions = Permission.objects.filter(codename__in = [ 'add_topic', ]) group.permissions.add(*permissions) def add_api_user

Django 1.7 - makemigrations creating migration for unmanaged model

落爺英雄遲暮 提交于 2019-12-03 09:48:15
I am creating some dynamic Django models in my application and everything seems to be working as expected except for the migration system. If I create a dynamic Django model and set managed = False, Django's makemigrations command still generates a migration for that new model. The migration looks something like this: class Migration(migrations.Migration): dependencies = [ ('atom', '0001_initial'), ] operations = [ migrations.CreateModel( name='books', fields=[ ], options={ 'db_table': 'books', 'managed': False, }, bases=(models.Model,), ), ] If I don't create the migration, when I run python

Django migrations with multiple databases

谁说我不能喝 提交于 2019-12-03 08:58:23
问题 I have a hard time with creating data migrations. I use two databases for my apps. I configured databases in settings.py and also created a router like in Django docs. # settings.py DB_HOST = 'localhost' DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'helios', 'HOST': DB_HOST, 'OPTIONS': { 'read_default_file': join(dirname(__file__), 'default.cnf'), }, }, 'other': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'gala_pol', 'HOST': DB_HOST, 'OPTIONS': { 'read_default

Check for pending Django migrations

﹥>﹥吖頭↗ 提交于 2019-12-03 06:38:48
问题 In Django, is there an easy way to check whether all database migrations have been run? I've found manage.py migrate --list , which gives me the information I want, but the format isn't very machine readable. For context: I have a script that shouldn't start running until the database has been migrated. For various reasons, it would be tricky to send a signal from the process that's running the migrations. So I'd like to have my script periodically check the database to see if all the

Add non-null and unique field with already populated model

邮差的信 提交于 2019-12-03 04:47:56
I have one model in my app running in a server with a few entries. I need to add a SlugField , unique and not-null for this model. The SlugField will be populated based on trading_name . I've changed my model in order to add this new field and modified save method: class Supplier(StatusModel): SLUG_MAX_LENGTH = 210 slug = models.SlugField(unique=True, max_length=SLUG_MAX_LENGTH) trading_name = models.CharField(max_length=200, verbose_name=_('trading name')) ... def save(self, *args, **kwargs): self.slug = orig = slugify(self.trading_name)[:Supplier.SLUG_MAX_LENGTH] for x in itertools.count(1):

Django 1.7 migration error

*爱你&永不变心* 提交于 2019-12-02 20:17:38
问题 I changed a field from CharField to ForeignKey on a Model called Availability, when I am trying to migrate I keep getting the error below: ValueError: Lookup failed for model referenced by field reservation.Availability.location: useraccount.Location Any idea why this could be happening? Thanks --------------UPDATED CODE-------------- App: reservation from useraccount.models import Location class Availability(models.Model): location = models.ForeignKey(Location) App: useraccount class

Django 1.7 migration error

為{幸葍}努か 提交于 2019-12-02 08:51:22
I changed a field from CharField to ForeignKey on a Model called Availability, when I am trying to migrate I keep getting the error below: ValueError: Lookup failed for model referenced by field reservation.Availability.location: useraccount.Location Any idea why this could be happening? Thanks --------------UPDATED CODE-------------- App: reservation from useraccount.models import Location class Availability(models.Model): location = models.ForeignKey(Location) App: useraccount class Location(models.Model): town = models.CharField(max_length=100) county = models.CharField(max_length=100) def

Error during Django “Running migrations”: django.db.utils.OperationalError: (2013, 'Lost connection to MySQL server during query')

丶灬走出姿态 提交于 2019-12-02 01:59:45
问题 My Django project is connect to the remote MySQL database. After I created all the model classes in Django, I tried python manage.py makemigrations Everything is fine. Then I entered python manage.py migrate Then an error occurred during "Running migrations:", "Applying event.0002_auto_20150904_2141..." Error code: django.db.utils.OperationalError: (2013, 'Lost connection to MySQL server during query') Does anyone know what might cause this error? I followed the exact procedures in the

Error during Django “Running migrations”: django.db.utils.OperationalError: (2013, 'Lost connection to MySQL server during query')

血红的双手。 提交于 2019-12-01 23:54:54
My Django project is connect to the remote MySQL database. After I created all the model classes in Django, I tried python manage.py makemigrations Everything is fine. Then I entered python manage.py migrate Then an error occurred during "Running migrations:", "Applying event.0002_auto_20150904_2141..." Error code: django.db.utils.OperationalError: (2013, 'Lost connection to MySQL server during query') Does anyone know what might cause this error? I followed the exact procedures in the official Django tutorial. Please do this : 1- Install new fresh of mySQL server first. 2- Create new db 3-

How to write migration to change primary key of model with ManyToManyField

梦想的初衷 提交于 2019-12-01 19:40:33
I have a UserProfile model that refers to my User model with a OneToOneField . I also use the post_save signal to auto create the UserProfile when the user is created. This works great apart from when creating a user through the admin (where I use an inline) when I get an error about duplicate profile. This answer recommends setting the primary key to be the OneToOneField referring to user . So before: class UserProfile(models.Model): user = models.OneToOneField(settings.AUTH_USER_MODEL) # ... subjects = models.ManyToManyField(Subject, null=True, blank=True) After class UserProfile(models