Hola,
I'm having trouble getting a subclass to trigger an isinstance(). Code below:
model.py excerpt:
------------
from django.db import models
from django.forms import ModelForm
class Author(models.Model):
first = models.CharField(u'First Name', max_length=20)
other = models.CharField(u'Other Names', max_length=20, blank=True)
last = models.CharField(u'Last Name', max_length=20)
class Meta:
ordering = ['last']
def __unicode__(self):
return u'%s %s' % (self.first, self.last)
def get_absolute_url(self):
return "/author/%i" % self.str()
class Translator(Author):
language = models.CharField(max_length=10, choices=LANGUAGES)
class Meta:
verbose_name = 'Translator'
verbose_name_plural = 'Translators'
------------
views.py excerpt:
------------
from django.shortcuts import render_to_response, get_object_or_404
from booktrans.books.models import *
from django.template import RequestContext
from django.http import HttpResponseRedirect
def index(request):
all_authors = Author.objects.all()
all_origAuthors = []
all_translators = []
for author in all_authors:
if isinstance(author, Translator):
all_translators.append(author)
else:
all_origAuthors.append(author)
return render_to_response('books/index.html',locals())
------------
And from the shell:
>>> from booktrans.books.models import *
>>> t = Translator.objects.all()
>>> t
[<Translator: qwerqwad asdasd>, <Translator: Yoko Miyagi>]
>>> t = Translator.objects.get(pk=2)
>>> t
<Translator: Yoko Miyagi>
>>> isinstance(t, Translator)
True
>>> a = Author.objects.get(pk=1)
>>> isinstance(a, Translator)
False
Any tips?
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.