%global _empty_manifest_terminate_build 0 Name: python-django-typed-models Version: 0.13.0 Release: 1 Summary: Sane single table model inheritance for Django License: BSD License URL: http://github.com/craigds/django-typed-models Source0: https://mirrors.nju.edu.cn/pypi/web/packages/bb/4a/83b9d625c29c78ff943c526f4f684b2a81af08823227c542f506b8149ccc/django-typed-models-0.13.0.tar.gz BuildArch: noarch %description # django-typed-models ![example workflow](https://github.com/craigds/django-typed-models/actions/workflows/tests.yml/badge.svg) ## Intro `django-typed-models` provides an extra type of model inheritance for Django. It is similar to single-table inheritance in Ruby on Rails. The actual type of each object is stored in the database, and when the object is retrieved it is automatically cast to the correct model class. Licensed under the New BSD License. ## Features * Models in querysets have the right class automatically * All models subclassing a common base are stored in the same table * Object types are stored in a 'type' field in the database * No extra queries or joins to retrieve multiple types ## Usage: An example says a bunch of words: ```python # myapp/models.py from django.db import models from typedmodels.models import TypedModel class Animal(TypedModel): """ Abstract model """ name = models.CharField(max_length=255) def say_something(self): raise NotImplemented def __repr__(self): return u'<%s: %s>' % (self.__class__.__name__, self.name) class Canine(Animal): def say_something(self): return "woof" class Feline(Animal): mice_eaten = models.IntegerField( default = 0 ) def say_something(self): return "meoww" ``` Later: ```python >>> from myapp.models import Animal, Canine, Feline >>> Feline.objects.create(name="kitteh") >>> Feline.objects.create(name="cheetah") >>> Canine.objects.create(name="fido") >>> print Animal.objects.all() [, , ] >>> print Canine.objects.all() [] >>> print Feline.objects.all() [, ] ``` You can actually change the types of objects. Simply run an update query: ```python Feline.objects.update(type='myapp.bigcat') ``` If you want to change the type of an object without refreshing it from the database, you can call ``recast``: ```python kitty.recast(BigCat) # or kitty.recast('myapp.bigcat') kitty.save() ``` ## Listing subclasses Occasionally you might need to list the various subclasses of your abstract type. One current use for this is connecting signals, since currently they don't fire on the base class (see [#1](https://github.com/craigds/django-typed-models/issues/1)) ```python for sender in Animal.get_type_classes(): post_save.connect(on_animal_saved, sender=sender) ``` ## Django admin If you plan to use typed models with Django admin, consider inheriting from typedmodels.admin.TypedModelAdmin. This will hide the type field from subclasses admin by default, and allow to create new instances from the base class admin. ```python from django.contrib import admin from typedmodels.admin import TypedModelAdmin from .models import Animal, Canine, Feline @admin.register(Animal) class AnimalAdmin(TypedModelAdmin): pass @admin.register(Canine) class CanineAdmin(TypedModelAdmin): pass @admin.register(Feline) class FelineAdmin(TypedModelAdmin): pass ``` ## Limitations * Since all objects are stored in the same table, all fields defined in subclasses are nullable. * Fields defined on subclasses can only be defined on *one* subclass, unless the duplicate fields are exactly identical. ## Requirements * Django 3.1+ * Python 3.6+ %package -n python3-django-typed-models Summary: Sane single table model inheritance for Django Provides: python-django-typed-models BuildRequires: python3-devel BuildRequires: python3-setuptools BuildRequires: python3-pip %description -n python3-django-typed-models # django-typed-models ![example workflow](https://github.com/craigds/django-typed-models/actions/workflows/tests.yml/badge.svg) ## Intro `django-typed-models` provides an extra type of model inheritance for Django. It is similar to single-table inheritance in Ruby on Rails. The actual type of each object is stored in the database, and when the object is retrieved it is automatically cast to the correct model class. Licensed under the New BSD License. ## Features * Models in querysets have the right class automatically * All models subclassing a common base are stored in the same table * Object types are stored in a 'type' field in the database * No extra queries or joins to retrieve multiple types ## Usage: An example says a bunch of words: ```python # myapp/models.py from django.db import models from typedmodels.models import TypedModel class Animal(TypedModel): """ Abstract model """ name = models.CharField(max_length=255) def say_something(self): raise NotImplemented def __repr__(self): return u'<%s: %s>' % (self.__class__.__name__, self.name) class Canine(Animal): def say_something(self): return "woof" class Feline(Animal): mice_eaten = models.IntegerField( default = 0 ) def say_something(self): return "meoww" ``` Later: ```python >>> from myapp.models import Animal, Canine, Feline >>> Feline.objects.create(name="kitteh") >>> Feline.objects.create(name="cheetah") >>> Canine.objects.create(name="fido") >>> print Animal.objects.all() [, , ] >>> print Canine.objects.all() [] >>> print Feline.objects.all() [, ] ``` You can actually change the types of objects. Simply run an update query: ```python Feline.objects.update(type='myapp.bigcat') ``` If you want to change the type of an object without refreshing it from the database, you can call ``recast``: ```python kitty.recast(BigCat) # or kitty.recast('myapp.bigcat') kitty.save() ``` ## Listing subclasses Occasionally you might need to list the various subclasses of your abstract type. One current use for this is connecting signals, since currently they don't fire on the base class (see [#1](https://github.com/craigds/django-typed-models/issues/1)) ```python for sender in Animal.get_type_classes(): post_save.connect(on_animal_saved, sender=sender) ``` ## Django admin If you plan to use typed models with Django admin, consider inheriting from typedmodels.admin.TypedModelAdmin. This will hide the type field from subclasses admin by default, and allow to create new instances from the base class admin. ```python from django.contrib import admin from typedmodels.admin import TypedModelAdmin from .models import Animal, Canine, Feline @admin.register(Animal) class AnimalAdmin(TypedModelAdmin): pass @admin.register(Canine) class CanineAdmin(TypedModelAdmin): pass @admin.register(Feline) class FelineAdmin(TypedModelAdmin): pass ``` ## Limitations * Since all objects are stored in the same table, all fields defined in subclasses are nullable. * Fields defined on subclasses can only be defined on *one* subclass, unless the duplicate fields are exactly identical. ## Requirements * Django 3.1+ * Python 3.6+ %package help Summary: Development documents and examples for django-typed-models Provides: python3-django-typed-models-doc %description help # django-typed-models ![example workflow](https://github.com/craigds/django-typed-models/actions/workflows/tests.yml/badge.svg) ## Intro `django-typed-models` provides an extra type of model inheritance for Django. It is similar to single-table inheritance in Ruby on Rails. The actual type of each object is stored in the database, and when the object is retrieved it is automatically cast to the correct model class. Licensed under the New BSD License. ## Features * Models in querysets have the right class automatically * All models subclassing a common base are stored in the same table * Object types are stored in a 'type' field in the database * No extra queries or joins to retrieve multiple types ## Usage: An example says a bunch of words: ```python # myapp/models.py from django.db import models from typedmodels.models import TypedModel class Animal(TypedModel): """ Abstract model """ name = models.CharField(max_length=255) def say_something(self): raise NotImplemented def __repr__(self): return u'<%s: %s>' % (self.__class__.__name__, self.name) class Canine(Animal): def say_something(self): return "woof" class Feline(Animal): mice_eaten = models.IntegerField( default = 0 ) def say_something(self): return "meoww" ``` Later: ```python >>> from myapp.models import Animal, Canine, Feline >>> Feline.objects.create(name="kitteh") >>> Feline.objects.create(name="cheetah") >>> Canine.objects.create(name="fido") >>> print Animal.objects.all() [, , ] >>> print Canine.objects.all() [] >>> print Feline.objects.all() [, ] ``` You can actually change the types of objects. Simply run an update query: ```python Feline.objects.update(type='myapp.bigcat') ``` If you want to change the type of an object without refreshing it from the database, you can call ``recast``: ```python kitty.recast(BigCat) # or kitty.recast('myapp.bigcat') kitty.save() ``` ## Listing subclasses Occasionally you might need to list the various subclasses of your abstract type. One current use for this is connecting signals, since currently they don't fire on the base class (see [#1](https://github.com/craigds/django-typed-models/issues/1)) ```python for sender in Animal.get_type_classes(): post_save.connect(on_animal_saved, sender=sender) ``` ## Django admin If you plan to use typed models with Django admin, consider inheriting from typedmodels.admin.TypedModelAdmin. This will hide the type field from subclasses admin by default, and allow to create new instances from the base class admin. ```python from django.contrib import admin from typedmodels.admin import TypedModelAdmin from .models import Animal, Canine, Feline @admin.register(Animal) class AnimalAdmin(TypedModelAdmin): pass @admin.register(Canine) class CanineAdmin(TypedModelAdmin): pass @admin.register(Feline) class FelineAdmin(TypedModelAdmin): pass ``` ## Limitations * Since all objects are stored in the same table, all fields defined in subclasses are nullable. * Fields defined on subclasses can only be defined on *one* subclass, unless the duplicate fields are exactly identical. ## Requirements * Django 3.1+ * Python 3.6+ %prep %autosetup -n django-typed-models-0.13.0 %build %py3_build %install %py3_install install -d -m755 %{buildroot}/%{_pkgdocdir} if [ -d doc ]; then cp -arf doc %{buildroot}/%{_pkgdocdir}; fi if [ -d docs ]; then cp -arf docs %{buildroot}/%{_pkgdocdir}; fi if [ -d example ]; then cp -arf example %{buildroot}/%{_pkgdocdir}; fi if [ -d examples ]; then cp -arf examples %{buildroot}/%{_pkgdocdir}; fi pushd %{buildroot} if [ -d usr/lib ]; then find usr/lib -type f -printf "/%h/%f\n" >> filelist.lst fi if [ -d usr/lib64 ]; then find usr/lib64 -type f -printf "/%h/%f\n" >> filelist.lst fi if [ -d usr/bin ]; then find usr/bin -type f -printf "/%h/%f\n" >> filelist.lst fi if [ -d usr/sbin ]; then find usr/sbin -type f -printf "/%h/%f\n" >> filelist.lst fi touch doclist.lst if [ -d usr/share/man ]; then find usr/share/man -type f -printf "/%h/%f.gz\n" >> doclist.lst fi popd mv %{buildroot}/filelist.lst . mv %{buildroot}/doclist.lst . %files -n python3-django-typed-models -f filelist.lst %dir %{python3_sitelib}/* %files help -f doclist.lst %{_docdir}/* %changelog * Fri May 05 2023 Python_Bot - 0.13.0-1 - Package Spec generated