util
class UploadTo:
def __init__(self, folder, filename_suffix=""):
self.folder = folder
self.filename_suffix = filename_suffix
def _get_filename(self, instance, filename):
_, file_extension = os.path.splitext(filename)
result = str(instance.a_uuid)
if self.filename_suffix:
result += "-{}".format(self.filename_suffix)
result += file_extension
return result
def save_path(self, instance, filename):
tmp_filename = self._get_filename(instance, filename)
result = "{}/{}".format(self.folder.value,
tmp_filename)
return result
model
class Sound(UuidMixin,
PhraseMixin,
CommentMixin):
british_sound = models.FileField(blank=True,
null=False,
default="",
upload_to=UploadTo(folder=UPLOAD_TO.VOCABULARY_SOUND_FOLDER,
filename_suffix="british").save_path)
american_sound = models.FileField(blank=True,
null=False,
default="",
upload_to=UploadTo(folder=UPLOAD_TO.VOCABULARY_SOUND_FOLDER,
filename_suffix="american").save_path)
Message
Migrations for 'vocabulary_sounds':
vocabulary_sounds/migrations/0009_alter_sound_american_sound_alter_sound_british_sound.py
- Alter field american_sound on sound
- Alter field british_sound on sound
Migration
class Migration(migrations.Migration):
dependencies = [
('vocabulary_sounds', '0008_alter_sound_american_sound_alter_sound_british_sound'),
]
operations = [
migrations.AlterField(
model_name='sound',
name='american_sound',
field=models.FileField(blank=True, default='', upload_to=general.model_aux.UploadTo.save_path),
),
migrations.AlterField(
model_name='sound',
name='british_sound',
field=models.FileField(blank=True, default='', upload_to=general.model_aux.UploadTo.save_path),
),
]
Copy Snippet
Edit Snippet
Wordwrap
class Sound(UuidMixin,
PhraseMixin,
CommentMixin):
british_sound = models.FileField(blank=True,
null=False,
default="",
upload_to=UploadTo(folder=UPLOAD_TO.VOCABULARY_SOUND_FOLDER,
filename_suffix="british").save_path)
american_sound = models.FileField(blank=True,
null=False,
default="",
upload_to=UploadTo(folder=UPLOAD_TO.VOCABULARY_SOUND_FOLDER,
filename_suffix="american").save_path)
Migrations for 'vocabulary_sounds':
vocabulary_sounds/migrations/0009_alter_sound_american_sound_alter_sound_british_sound.py
- Alter field american_sound on sound
- Alter field british_sound on sound
class Migration(migrations.Migration):
dependencies = [
('vocabulary_sounds', '0008_alter_sound_american_sound_alter_sound_british_sound'),
]
operations = [
migrations.AlterField(
model_name='sound',
name='american_sound',
field=models.FileField(blank=True, default='', upload_to=general.model_aux.UploadTo.save_path),
),
migrations.AlterField(
model_name='sound',
name='british_sound',
field=models.FileField(blank=True, default='', upload_to=general.model_aux.UploadTo.save_path),
),
]
Whenever I carry out makemigrations, this code generates a new migration.
Literally:
python manage.py makemigrations
A new migration appears.
Then at once:
python manage.py makemigrations
A new migration appears.
The text of migrations is always the same (as far as I can see).
Why do new migrations appear though the model is intact? And how to fix the problem?
Related
I did lot's of try, catch on this issue and basically still not find the core problem but this is what I got for now.
in the settings.py I am turning on the USE_TZ = True TIME_ZONE = 'UTC' and creating a model like below.
class Plan(models.Model):
name = models.CharField(max_length=100)
code = models.CharField(max_length=100)
price = models.IntegerField()
description = models.TextField(null=True, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.name
when I try to makemigration for this is no problem. Here is the first migration file of Plan model
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Plan',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=100)),
('code', models.CharField(max_length=100)),
('price', models.IntegerField()),
('description', models.TextField(blank=True, null=True)),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
],
),
]
Now, when I try to migrate this file. There are two table created in the DB. one for django_migrations and the other is Plan model. After running migrate command. error follows.
raise ValueError('Not naive datetime (tzinfo is already set)') ValueError: Not naive datetime (tzinfo is already set)
first plan model causing this error. But when I delete the django_migrations table from DB. error not recurring. it's look like even if I use USE_TZ = True django not inserting date correctly.
I extend the Django user model like this:
#core.models
class Institute(models.Model):
phone_regex = RegexValidator(regex=r'^\+?1?\d{9,15}$', message=institute_phone_help_text)
name = models.CharField(_('name'), max_length=255)
description = models.TextField(_('description'), blank=True)
abbreviation = models.CharField(_('abbreviation'), blank=True, max_length=100)
address = models.TextField(_('address'), blank=True)
phone = models.CharField(validators=[phone_regex], max_length=17, blank=True) # validators should be a list
websites = ArrayField(models.URLField(max_length=255), verbose_name=_('websites'), blank=True, null=True)
class Meta:
verbose_name = _('institute')
verbose_name_plural = _('institutes')
def __str__(self):
return '{0} ({1})'.format(self.name, self.abbreviation)
class User(AbstractUser):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
institute = models.ForeignKey(Institute, on_delete=models.CASCADE)
params = JSONField(_('params'), null=True, blank=True,)
about_me = models.TextField(_('about me'), blank=True,)
With an empty DB each time that I launch ./manage.py makemigrations core it creates always a new migration file
import django.contrib.auth.models
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('core', '0002_auto_20190430_1655'),
]
operations = [
migrations.AlterModelManagers(
name='user',
managers=[
('objects', django.contrib.auth.models.UserManager()),
],
),
]
I tried different combinations:
./manage.py makemigrations core
./manage.py migrate core
./manage.py migrate
./manage.py makemigrations core
./manage.py makemigrations core
./manage.py makemigrations
./manage.py migrate
It always creates the migration file.
Thanks.
D
if you use AbstractUser model make sure you run makemigrations before migrate.
i had same issue with AbstractUser,
I solved it by deleting the sqlite3 file also all migrations files.
After that i run this two commands :
python manage.py makemigrations
python manage.py migrate
inside your terminal
Let me know if that help!
I am trying to upload some testing data to an app, using the option of the migrations.All the data is storaged in a .Yaml file in the app ans I have some other migrations that runs perfectly uploading all the data.
But this one has a problem. In this model (Transactions) I created 3 self-writen, fields that are calculated when calling to the save() method. This process run perfectly when I sent the data through the View. But when I send it through the migration, fails as if the save method is not overwritten. I don't know what to do to accomplish the upload as a migration.
The migration
from django.db import migrations
from django.core import serializers
def transactions(apps, schema_editor):
with open('fixtures/transactions.yaml') as trans:
for obj in serializers.deserialize("yaml", trans):
t=apps.get_model("acounts", "Transactions")()
cat=apps.get_model("acounts","Category")
.objects.get(pk=obj.object.category.pk)
cuen=apps.get_model("acounts", "Acount").objects.get(pk=obj.object.acount.pk)
print(obj)
t.tipo=obj.object.tipo
t.description=obj.object.description
t.monto=obj.object.monto
t.date=obj.object.date
# t.category=obj.object.category
t.category=cat
# t.acount=obj.object.acount
t.acount=cuen
t.save()
class Migration(migrations.Migration):
dependencies = [
('acounts', '0002_populate_acounts'),
]
operations = [
(migrations.RunPython(transactions))
]
The Model
class Transactions(models.Model):
TYPE_CHOICES = (
('GASTO', 'Gasto'),
('INGRESO', 'Ingreso'),
)
tipo = models.CharField(
choices=TYPE_CHOICES,
max_length=20
)
description=models.CharField(max_length=100)
monto=models.DecimalField(max_digits=25,
decimal_places=2,
null=False)
category=models.ForeignKey('Category',
on_delete=models.CASCADE,
null=False)
acount=models.ForeignKey('Acount',
on_delete=models.CASCADE,
null=False)
date=models.DateField()
total_USD=models.DecimalField(
max_digits=25,
decimal_places=2,
editable=False);
total_BTC=models.DecimalField(
max_digits=25,
decimal_places=9,
editable=False);
total_BSS=models.DecimalField(
max_digits=25,
decimal_places=2,
editable=False);
total_EUR=models.DecimalField(
max_digits=25,
decimal_places=2,
editable=False);
created_at=models.DateTimeField(
auto_now_add=True,
editable=False)
updated_at=models.DateTimeField(
auto_now=True,
editable=False)
def save(self):
cotizations=Currency.objects.all()
currency=self.acount.currency.name
in_usd=self.monto/Currency.objects.get(name=currency).price_USD_now
query_btc=in_usd*Currency.objects.get(name='BTC').price_USD_now
query_bss=in_usd*Currency.objects.get(name='YEN').price_USD_now
query_eur=in_usd*Currency.objects.get(name='EUR').price_USD_now
self.total_USD=in_usd
self.total_BTC=query_btc
self.total_YEN=query_bss
self.total_EUR=query_eur
super(Transactions, self).save()
return query_btc;
The Error
raise utils.IntegrityError(*tuple(e.args))
django.db.utils.IntegrityError: (1048, "Column 'total_USD' cannot be null
The same method runs perfect when done trough the view, How could I create a data migration for this model using the overwritten .save() method?
I am trying to create a one-to-many relationship between the YahooTickerSymbol and PriceHistory database. However, I already have some data in the PriceHistory database without any keys relating to the YahooTickerSymbol database. Is there a way to create the relationship without violating the foreign key constraint?
class YahooTickerSymbols(models.Model):
yahoo_ticker_number = models.AutoField(primary_key=True, )
yahoo_ticker_symbol = models.CharField(max_length=20, )
yahoo_company_name = models.CharField(max_length=120, )
class Meta:
ordering = ['yahoo_company_name', ]
verbose_name_plural = "Yahoo Ticker Symbols"
def __str__(self):
return "%s is %s." % (self.yahoo_company_name, self.yahoo_ticker_symbol)
class PriceHistory(models.Model):
price_data_number = models.AutoField(primary_key=True, )
yahoo_ticker_symbol = models.CharField(max_length=20, )
price_date = models.DateField(auto_now=False, auto_now_add=False, )
price_open = models.DecimalField(max_digits=7, decimal_places=3, )
price_high = models.DecimalField(max_digits=7, decimal_places=3, )
price_low = models.DecimalField(max_digits=7, decimal_places=3, )
price_close = models.DecimalField(max_digits=7, decimal_places=3, )
price_adjusted_close = models.DecimalField(max_digits=7, decimal_places=3, )
price_volume = models.BigIntegerField()
yahootickersymbol = models.ForeignKey(YahooTickerSymbols, blank=True, null=True,
on_delete=models.SET_NULL, )
class Meta:
ordering = ['yahoo_ticker_symbol', 'price_date', ]
verbose_name_plural = "Historical Prices"
def __str__(self):
return "%s - %s : %s" % (self.yahoo_ticker_symbol, self.price_date, self.price_close)
The code in the migration file is as follows:
class Migration(migrations.Migration):
dependencies = [
('investments', '0009_auto_20160124_1517'),
]
operations = [
migrations.AlterField(
model_name='pricehistory',
name='yahootickersymbol',
field=models.ForeignKey(to='investments.YahooTickerSymbols', on_delete=django.db.models.deletion.SET_NULL, null=True, blank=True),
),
]
I realized that I made a mistake in the process of running the makemigrations command.
Previously, I did not set null=True for models.ForeignKey and I ran the makemigrations command. After realizing my error, I included null=True within models.py. However, I did not delete the previous migration files. As such, Django was stuck in trying to run the old migration files even though these were not possible to be effected.
To resolve this, you need to run showmigrations to understand which migration files have not been included to understand the root of the problem.
I am creating a press page with articles etc.
So, an article has a source (category). And then multiple article can have the same source or many.
I divided it in two classes :
#version_controlled_content
#python_2_unicode_compatible
class ArticleSource(TranslatableModel):
taints_cache = True
date_created = models.DateTimeField(_('created at'), auto_now_add=True)
date_modified = models.DateTimeField(_('modified at'), auto_now=True)
position = models.PositiveIntegerField(default=0, blank=False, null=False)
translations = TranslatedFields(
name=models.CharField(_('name'), max_length=510),
slug=models.SlugField(_('slug'), blank=True, db_index=True),
meta={'unique_together': (('language_code', 'slug'),)},
)
objects = TranslationManager()
class Meta:
verbose_name = _('Press source')
verbose_name_plural = _('Press sources')
ordering = ('position', )
#property
def count(self):
return self.press_article.filter(publish=True).count()
def __str__(self):
name = self.safe_translation_getter('name', any_language=True)
return name if name is not None else '(not translated)'
def save(self, *args, **kwargs):
super(ArticleSource, self).save(*args, **kwargs)
for lang in self.get_available_languages():
self.set_current_language(lang)
if not self.slug and self.name:
self.slug = slugify(force_text(self.name))
self.save_translations()
#version_controlled_content
#python_2_unicode_compatible
class Article(ModelMeta, TranslatableModel):
taints_cache = True
"""
Press article element,
"""
date_created = models.DateTimeField(auto_now_add=True)
date_modified = models.DateTimeField(auto_now=True)
date_realization = models.DateField(_('Realised in'),
default=timezone.now)
image = FilerImageField(verbose_name=_('Featured image'), blank=True,
null=True,
on_delete=models.SET_NULL,
related_name='image_press_article',
help_text=_('Set if the article will be featured'))
sources = models.ManyToManyField(ArticleSource, verbose_name=_('Source'),
blank=False, null=True, related_name='sources_press_article')
regions = models.ManyToManyField(Country, verbose_name=_('Country of the article'),
blank=True, null=True,
related_name='regions_press_article')
global_regions = models.BooleanField('Global', default=False)
featureArticle = models.BooleanField('Feature this article', default=False)
sites = models.ManyToManyField(Site, verbose_name=_('Sites'), blank=True,
null=True,
help_text=_('Select sites in which show the project.'))
article_url = models.CharField(_('Article Url'), max_length=310, blank=False,
help_text=_('Use to link to the original source'))
position = models.PositiveIntegerField(default=0, blank=False, null=False)
....
The admin is a standard one :
class ArticleAdmin(MultisiteAdminMixin,
VersionedPlaceholderAdminMixin,
FrontendEditableAdminMixin,
SortableAdminMixin,
TranslatableAdmin):
def sites_visible_on(self, obj):
return ', ' .join('%s' % site.name for site in obj.sites.all())
def countries_visible_on(self, obj):
qs = obj.countries_displayed_in.all()
if not len(qs):
return _('All')
return ', ' .join('%s' % country.name
for country in obj.countries_displayed_in.all())
sites_visible_on.short_description = _('Sites visible on')
countries_visible_on.short_description = _('Countries visible from')
list_display = [
'__str__', 'date_realization',
'sites_visible_on', 'countries_visible_on',
'language_column',
'featureArticle'
]
search_fields = ('translations__title',)
date_hierarchy = 'date_realization'
frontend_editable_fields = ('title', 'source', 'description')
list_per_page = 10
fieldsets = [
('Archive article', {
'fields': [('title', 'sources'), ('slug', 'date_realization'), ('regions', 'global_regions'), 'article_url']
}),
('Feature', {
'fields': ('featureArticle', 'image', 'description'),
}),
('Localization', {
'fields': ('sites', 'countries_displayed_in'),
'classes': ('collapse',)
}),
('SEO', {
'fields': [('meta_description', 'meta_title', 'meta_keywords')],
'classes': ('collapse',)
}),
]
def get_prepopulated_fields(self, request, obj=None):
return {'slug': ('title',)}
class ArticleSourceAdmin(VersionedPlaceholderAdminMixin,
FrontendEditableAdminMixin,
SortableAdminMixin,
TranslatableAdmin):
list_display = ['__str__', 'language_column']
def get_prepopulated_fields(self, request, obj=None):
return {'slug': ('name',)}
admin.site.register(ArticleSource, ArticleSourceAdmin)
admin.site.register(Article, ArticleAdmin)
So, everything is displaying correctly except 2 case :
If I want to add an Article I will have :
Relation "djangocms_press_articlesource" does not exist
LINE 1: ..., "djangocms_press_articlesource"."position" FROM "djangocms...
If I create a new source and save it, I have the same error showing
So I start to feel without option, the migration file seems to be correct about articleSource... :
migrations.CreateModel(
name='ArticleSource',
fields=[
('id', models.AutoField(serialize=False, primary_key=True, verbose_name='ID', auto_created=True)),
('date_created', models.DateTimeField(auto_now_add=True, verbose_name='created at')),
('date_modified', models.DateTimeField(auto_now=True, verbose_name='modified at')),
('position', models.PositiveIntegerField(default=0)),
],
options={
'verbose_name_plural': 'Press sources',
'ordering': ('position',),
'verbose_name': 'Press source',
},
bases=(models.Model,),
),
Well, I am completely lost, it was working fine this way with a showroom application, but not this time apparently.
Today is the day of migrations' woes - I've just answered a very similar question.
So, in your case Django somehow thinks it already created the tables, but in fact it didn't. To re-run migrations, you should make Django think it has not yet run any migrations by executing:
manage.py migrate yourapp zero --fake
Then apply your migrations one-by-one to track any errors:
manage.py migrate yourapp 0001
manage.py migrate yourapp 0002
and so on. Be careful though, as you very probably could lose your data. Backup always does the trick.