mirror of
https://github.com/SectorLabs/django-localized-fields.git
synced 2025-04-25 11:42:54 +03:00
Fix crash when using LocalizedUniqueSlugField in a bulk_create
This commit is contained in:
parent
1b036dc1de
commit
b97a7f3c23
@ -91,11 +91,12 @@ class LocalizedUniqueSlugField(LocalizedAutoSlugField):
|
|||||||
if self.include_time:
|
if self.include_time:
|
||||||
slug += '-%d' % datetime.now().microsecond
|
slug += '-%d' % datetime.now().microsecond
|
||||||
|
|
||||||
if instance.retries > 0:
|
retries = getattr(instance, 'retries', 0)
|
||||||
|
if retries > 0:
|
||||||
# do not add another - if we already added time
|
# do not add another - if we already added time
|
||||||
if not self.include_time:
|
if not self.include_time:
|
||||||
slug += '-'
|
slug += '-'
|
||||||
slug += '%d' % instance.retries
|
slug += '%d' % retries
|
||||||
|
|
||||||
slugs.set(lang_code, slug)
|
slugs.set(lang_code, slug)
|
||||||
|
|
||||||
|
@ -4,7 +4,7 @@ from django.db import models
|
|||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
|
|
||||||
from localized_fields.fields import LocalizedField
|
from localized_fields.fields import LocalizedField, LocalizedUniqueSlugField
|
||||||
|
|
||||||
from .data import get_init_values
|
from .data import get_init_values
|
||||||
from .fake_model import get_fake_model
|
from .fake_model import get_fake_model
|
||||||
@ -16,6 +16,9 @@ class LocalizedBulkTestCase(TestCase):
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def test_localized_bulk_insert():
|
def test_localized_bulk_insert():
|
||||||
|
"""Tests that bulk inserts work properly when using
|
||||||
|
a :see:LocalizedField in the model."""
|
||||||
|
|
||||||
model = get_fake_model(
|
model = get_fake_model(
|
||||||
'BulkInsertModel',
|
'BulkInsertModel',
|
||||||
{
|
{
|
||||||
@ -31,3 +34,25 @@ class LocalizedBulkTestCase(TestCase):
|
|||||||
])
|
])
|
||||||
|
|
||||||
assert model.objects.all().count() == 3
|
assert model.objects.all().count() == 3
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def test_localized_slug_bulk_insert():
|
||||||
|
"""Tests whether bulk inserts work properly when using
|
||||||
|
a :see:LocalizedUniqueSlugField in the model."""
|
||||||
|
|
||||||
|
model = get_fake_model(
|
||||||
|
'BulkSlugInsertModel',
|
||||||
|
{
|
||||||
|
'name': LocalizedField(),
|
||||||
|
'slug': LocalizedUniqueSlugField(populate_from='name', include_time=True),
|
||||||
|
'score': models.IntegerField()
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
objects = model.objects.bulk_create([
|
||||||
|
model(name={'en': 'english name 1', 'ro': 'romanian name 1'}, score=1),
|
||||||
|
model(name={'en': 'english name 2', 'ro': 'romanian name 2'}, score=2),
|
||||||
|
model(name={'en': 'english name 3', 'ro': 'romanian name 3'}, score=3)
|
||||||
|
])
|
||||||
|
|
||||||
|
assert model.objects.all().count() == 3
|
||||||
|
Loading…
x
Reference in New Issue
Block a user