inheritance, abstract base classes and generic foreign keys

16 views
Skip to first unread message

herr.k...@googlemail.com

unread,
Apr 9, 2009, 6:48:35 PM4/9/09
to Django users
Hi!

I have cages filled with animals. All animals can move, but they do it
all differently. Some animals even have attributes the others don't.
Now i want all the animals in a cage to move.

How do i implement that in Django? First i thought i could use simple
inheritance, serialize the attributes and save them to a TextField,
but i have no idea where to unserialize them at fetchtime. So i
figured that wouldn't be the best approach.

So what i found out what i really want is an abstract animal base
class. Now the Problem is how do i get them in the cage? I could use
generic foreign keys, but in this case i would need one for every
animal, which wouldn't work.
So what am i missing here? How is it intended to be?

Thanks for your help!
Daniel

Malcolm Tredinnick

unread,
Apr 9, 2009, 7:43:20 PM4/9/09
to django...@googlegroups.com

I have sat and thought about this for a while and I still have
absolutely no idea what you're asking. Could you forget about the
abstract stuff with animals and cages and show some small examples of
the code you're trying to write and an explanation of what isn't
working? Is an animal class meant to be linked to a cage class? Is a
cage even a class, or is it an attribute? Why was serializing ever an
option for relational model representation?

Unfortunately, you've over-generalised your problem to the degree that
it probably only makes sense to you now. :-)

Regards,
Malcolm

herr.k...@googlemail.com

unread,
Apr 10, 2009, 5:34:36 AM4/10/09
to Django users
Thanks Malcom.

All right lets see if i could put my real problem simpler. Lets say
you are the tutor of a programming course and you want to check the
programming assignments semi-automatically.
So you would set up a Task to which a student can submit a solution.
If the solution is accepted depends on various tests. For example does
the program compute the right output to a given input, is everything
well formated and right commented, is every occurrence of the students
name deleted from the code so that a reviewer that might know the
student can grade unbiased, etc.
These test are configureable for every assignment that uses them.
Every Test inherits a common interface(eg. run_test()) from the
abstract test base class.
Now the first step is to assign a bunch of tests to an assignment.
This is problematic because different tests are different classes/
tables. A task could have one test trough a generic foreign link, but
again only one. What i want to do is this:

for test in task.test_set()
test.run_test(solution)

where task.test_set() is the problematic part.

I hope that makes it a bit clearer.

Cage = Task
Animals = Tests
walk = run_test()

I think my analogy wasn't that bad.

Serialization of the test sub classes data members wouldn't be a
problem, because i access a test only in two cases: on creation an to
run run_test(). I would never need to join the checker configuration
against anything.
But yeah, i like the idea of having the configuration in human
readable form in the database much more.

Sorry that i cant post code. I don't have any that would explain it
more, because i haven't implemented any of the above ideas because
they all seam to be a dead end.

So if anybody could enlighten me,
that would be greatly appreciated.

Daniel



On Apr 10, 1:43 am, Malcolm Tredinnick <malc...@pointy-stick.com>
wrote:

Malcolm Tredinnick

unread,
Apr 10, 2009, 6:10:42 AM4/10/09
to django...@googlegroups.com
On Fri, 2009-04-10 at 02:34 -0700, herr.k...@googlemail.com wrote:
> Thanks Malcom.
>
> All right lets see if i could put my real problem simpler. Lets say
> you are the tutor of a programming course and you want to check the
> programming assignments semi-automatically.
> So you would set up a Task to which a student can submit a solution.
> If the solution is accepted depends on various tests. For example does
> the program compute the right output to a given input, is everything
> well formated and right commented, is every occurrence of the students
> name deleted from the code so that a reviewer that might know the
> student can grade unbiased, etc.
> These test are configureable for every assignment that uses them.
> Every Test inherits a common interface(eg. run_test()) from the
> abstract test base class.
> Now the first step is to assign a bunch of tests to an assignment.
> This is problematic because different tests are different classes/
> tables. A task could have one test trough a generic foreign link, but
> again only one.

You could probably do with one more level of indirection here.
Assignments can contain a one-to-many to a model that contains a generic
relation to the right type of test.

class Task(models.Model):
...

class Attempt(models.Model):
task = models.ForeignKey(Task)
content_type = models.ForeignKey(ContentType)
object_id = models.IntegerField()
test = models.GenericForeignKey()

The test / content_type / object_id collection point to your various
test classes. You can then run through them with:

for attempt in task.attempt_set.all():
attempt.test.run_test(...)

[...]


> I hope that makes it a bit clearer.
>
> Cage = Task
> Animals = Tests
> walk = run_test()
>
> I think my analogy wasn't that bad.

Whatever. When I said I couldn't understand it, I wasn't just making
idle conversation. It wasn't at all clear what were classes or models in
your original description. This time around, you talked about concrete
models and assigning something to something else using many-to-many
relations and so forth. It may well have made sense to you, but that's
the least of concerns when asking for help.

Regards,
Malcolm

herr.k...@googlemail.com

unread,
Apr 10, 2009, 3:00:49 PM4/10/09
to Django users


On Apr 10, 12:10 pm, Malcolm Tredinnick <malc...@pointy-stick.com>
wrote:
Thanks for your fast reply.

Thats a nice idea. Works great.
Now if i only could get the tests to appear inline in the task admin
page.
Do you think i could accomplish that by setting the formset in an
inlineModelAdmin to something else or do i think in the wrong
direction.

Greetings,
Daniel
Reply all
Reply to author
Forward
0 new messages