2

We have a generic super model class:

class UUIDObject(models.Model):
    id = SimpleUUIDField(primary_key=True, default=uuid.uuid4)

    objects = InheritanceManager()

UUIDObject uses InheritanceManager from model_utils and SimpleUUIDField which is a simple specialization of Django's UUIDField with more tolerance regarding string values.

UUIDObject permits retrieval of a model instance by id without having to worry about the model type.

The User model for the app inherits from UUIDObject thus:

from django.contrib.auth.models import AbstractUser, UserManager as DjangoUserManager
from model_utils.managers import InheritanceManager

class UserManager(InheritanceManager, DjangoUserManager):
    pass


class User(AbstractUser, UUIDObject):
    objects = UserManager()

To preserve development users over re-factors, we keep a fixture of the devs for convenience. This is readily created with e.g django-admin.py dumpdata core.user --indent 2 > devs.json and the resulting file looks correct. When reloaded with e.g django-admin.py loaddata devs, the loading of the fixtures is reported as successful but the objects do not get added to the tables. When I use the verbose flag, it indicates that each user record in the fixture is being added twice (that sorta makes sense - one for the root table and one for the dependent table) but no actual entries appear in either table.

It seems that loaddata is just failing silently and reporting success.

Paul Whipp
  • 16,028
  • 4
  • 42
  • 54
  • Isn't it more likely that the this is something to do with the third party app you are using – e4c5 Dec 12 '16 at 00:08
  • It may relate to the way that the django fixture processes the raw data (saving the model without using the object manager or normal save methods) but I'm not using any special save methods and the object managers are all about querysets rather than anything to do with object creation. – Paul Whipp Dec 12 '16 at 02:40

1 Answers1

0

I ran into the same issue with abstract class inherit from InheritanceManage cause abstract=True declaration was removed from model when using model_utils.

I try execute save() in post_save method and so far works:

from django.db.models.signals import post_save


class ProductA(AbstractProduct):
    pass


def instance_post_save(sender, instance, created, **kwargs):
    if kwargs.get('raw'):
        instance.save()
    return


post_save.connect(instance_post_save, sender=ProductA)
Travis
  • 1
  • That may have merit as a workaround but saving again in a post_save?? – Paul Whipp Jan 16 '18 at 01:45
  • I try to execute save() here because save() is not called by loaddata, https://stackoverflow.com/questions/8595536/does-a-models-save-method-get-called-when-using-loaddata-for-fixtures – Travis Jan 17 '18 at 03:47
  • maybe that's the reason why InheritanceManage subclass objects in fixtures not actually saved in database – Travis Jan 17 '18 at 03:58