Django: Create an instance of Manager with a copy of a custom QuerySet

Sometimes we want to query by custom filter and different orders

And in this case will raise AttributeError because Queryset object can not call Manager method

Solution: as_manager()

More Django settings from document:

發表留言

Django Unit Test with Fixtures

Sometimes we need to write test with initial data

For this setting as example, we can load initial data by

And we need to do setup in test cases

發表留言

Django: Query from abstract model class

Abstract classes such as AbstractProduct which inherit from third party package model_utils.InheritanceManage, will not work with abstract attribute specified true:

For this reason, its subclass like ProductA will cause create issue when load fixtures data by running this command:

Which will not raise errors also not writing into database, see the same issue on stackoverflow

The solution here is provide post_save signal and execute save(), because loaddata command will not call pre_save() and save()

發表留言