Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion typedmodels/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,12 @@ def do_related_class(self, other, cls):
if typ in base_class._typedmodels_registry:
raise ValueError("Can't register %s type %r to %r (already registered to %r )" % (typ, classname, base_class._typedmodels_registry))
base_class._typedmodels_registry[typ] = cls

type_name = getattr(cls._meta, 'verbose_name', cls.__name__)
type_field = base_class._meta.get_field('type')
type_field._choices = tuple(list(type_field.choices) + [(typ, type_name)])
choices = tuple(list(type_field.choices) + [(typ, type_name)])
choices_field = '_choices' if django.VERSION < (1, 9) else 'choices'
setattr(type_field, choices_field, choices)

cls._meta.declared_fields = declared_fields

Expand Down
4 changes: 4 additions & 0 deletions typedmodels/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ def test_get_type_classes(self):
self.assertEqual(set(Canine.get_type_classes()), set([Canine]))
self.assertEqual(set(Feline.get_type_classes()), set([BigCat, AngryBigCat, Feline]))

def test_type_choices(self):
type_choices = set((cls for cls, _ in Animal._meta.get_field('type').choices))
self.assertEqual(type_choices, set(Animal.get_types()))

def test_base_model_queryset(self):
# all objects returned
qs = Animal.objects.all().order_by('type')
Expand Down