overriding model field defaults in inherited model class with static methods
Hi. I'm trying to override the defaults of an text field in an subclass. I thought i could accomplish that by the following code, but models.TextField(default = default('flags')... wouldn't find default (). I cant reference it by self.__class__.default() as the is no self at the field initialization. Im kind of stuck ... Thank you for your help! class Compiler(Model): class Meta(Checker.Meta): abstract = True # default values for the model fields defaults = { 'flags' : "-Wall -static", 'output_flags' : "-o %s", 'libs' : "", 'file_pattern' : r"^[a-zA-Z0-9_]*$"# Regular expression describing all source files to be passed to the compiler. } def default(key): """ By making the model field defaults a callable instead of an value it will be called every time a new object is created wich will allow for altered defaults in subclasses. """ return defaults[key] _flags = models.TextField(default = default('flags'), help_text = _ ('Compiler flags')) _output_flags = models.TextField(default = default('output_flags'), help_text = _('Output flags')) _libs = models.TextField(default = default('libs'), help_text = _ ('Compiler libraries')) class CXXBuilder(compiler): """ A C++ compiler for construction. """ # override configuration _compiler = "c++" _language = "C++" # override default values for the model fields defaults['flags'] = "-Wall", defaults['output_flags']= "" defaults['file_pattern']= r"^[a-zA-Z0-9_]*\.(c|C|cc|CC|cxx|CXX|c\+\+| C\+\+|cpp|CPP)$" --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: inheritance, abstract base classes and generic foreign keys
On Apr 10, 12:10 pm, Malcolm Tredinnick wrote: > On Fri, 2009-04-10 at 02:34 -0700, herr.klein...@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 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 --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: inheritance, abstract base classes and generic foreign keys
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 wrote: > On Thu, 2009-04-09 at 15:48 -0700, herr.klein...@googlemail.com wrote: > > 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? > > 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 --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
inheritance, abstract base classes and generic foreign keys
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 --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---