Merge pull request #56 from sliverc/user_defined_pk_descriptor

Avoid DoesNotExist error when creating model with user defined pk
This commit is contained in:
Swen Kooij 2019-01-11 14:47:33 +02:00 committed by GitHub
commit 25417b5815
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 1 deletions

View File

@ -41,7 +41,7 @@ class LocalizedValueDescriptor:
if self.field.name in instance.__dict__:
value = instance.__dict__[self.field.name]
elif instance.pk is not None:
elif not instance._state.adding:
instance.refresh_from_db(fields=[self.field.name])
value = getattr(instance, self.field.name)
else:

View File

@ -1,6 +1,7 @@
import json
from django.conf import settings
from django.db import models
from django.db.utils import IntegrityError
from django.test import TestCase
@ -204,6 +205,16 @@ class LocalizedFieldTestCase(TestCase):
for field in field.formfield().fields:
assert field.required
def test_descriptor_user_defined_primary_key(self):
"""Tests that descriptor works even when primary key is user defined."""
model = get_fake_model(dict(
slug=models.SlugField(primary_key=True),
title=LocalizedField()
))
obj = model.objects.create(slug='test', title='test')
assert obj.title == 'test'
def test_required_all(self):
"""Tests whether passing required=True properly validates
that all languages are filled in."""