List Info

Thread: Hard coded instances of model classes




Hard coded instances of model classes
user name
2006-11-22 13:25:28
Greetings,

I'm working on an application which evaluates a bunch of
products 
according to some parameters. Parameters and the scales they
use can be 
defined by the user. A scale defines the actual scale values
as well as 
formulas for grading the product in different ways.

Now the thing is that some of the scales are so complicated
that it would 
be quite difficult to figure out a way for defining them via
the user 
interface. Therefore I'm considering using some predefined
scales that 
would actually be hard coded into the application. The code
in models.py 
might be something like this:


# ...
class Scale(models.Model):
 	fGrade1 = models.CharField('Formula for grade1',
maxlength=32)
 	# ...
 	def calcGrade1(...):
 		# use fGrade1 to calculate grade1
 	# ...

class SpecialScale1(...):
 	# ...
 	def calcGrade1(...):
 		# use some custom code to calculate grade1
 	# ...
# ...


The ideal solution would be such that would allow me to do:

 	Scale.objects.get(id=some_scale_id)

...which would, depending on the id, get either a user
defined scale from 
the DB (instance of Scale) or an instance of e.g.
SpecialScale1 (subclass 
of Scale?).

I guess the ideal solution is not possible to achieve
without tweaking 
Django. Any other ideas for a solution?

Thank you for your time,


Mikko (newbie)

--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Hard coded instances of model classes
user name
2006-11-22 13:40:28
Unfortunately, model inheritance is not supported at the
moment, so
SpecialScale1 can't be a subclass of Scale. If you want to
predefine
some Scale objects, you can just prepopulate the database
with known
values, i.e. writing a script load_data.py that would create
the
necessary objects and relationships (use
http://satchmo.python-hosting.com/file/trunk/sa
tchmo/load_data.py for
reference).

If you want to restrict the content administrators from
modifying
those records, you can use Django per-object-permission
branch for
that.

Hope, this helps you.
Good luck!
Aidas Bendoraitis




On 11/22/06, Mikko Suniala <mikko.sunialatut.fi> wrote:
>
> Greetings,
>
> I'm working on an application which evaluates a bunch
of products
> according to some parameters. Parameters and the scales
they use can be
> defined by the user. A scale defines the actual scale
values as well as
> formulas for grading the product in different ways.
>
> Now the thing is that some of the scales are so
complicated that it would
> be quite difficult to figure out a way for defining
them via the user
> interface. Therefore I'm considering using some
predefined scales that
> would actually be hard coded into the application. The
code in models.py
> might be something like this:
>
>
> # ...
> class Scale(models.Model):
>         fGrade1 = models.CharField('Formula for
grade1', maxlength=32)
>         # ...
>         def calcGrade1(...):
>                 # use fGrade1 to calculate grade1
>         # ...
>
> class SpecialScale1(...):
>         # ...
>         def calcGrade1(...):
>                 # use some custom code to calculate
grade1
>         # ...
> # ...
>
>
> The ideal solution would be such that would allow me to
do:
>
>         Scale.objects.get(id=some_scale_id)
>
> ...which would, depending on the id, get either a user
defined scale from
> the DB (instance of Scale) or an instance of e.g.
SpecialScale1 (subclass
> of Scale?).
>
> I guess the ideal solution is not possible to achieve
without tweaking
> Django. Any other ideas for a solution?
>
> Thank you for your time,
>
>
> Mikko (newbie)
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Hard coded instances of model classes
user name
2006-11-22 15:28:14
On Wed, 22 Nov 2006, Aidas Bendoraitis wrote:
> If you want to predefine some Scale objects, you can
just prepopulate 
> the database with known values, i.e. writing a script
load_data.py that 
> would create the necessary objects and relationships
(use 
> http://satchmo.python-hosting.com/file/trunk/sa
tchmo/load_data.py for 
> reference).

That was actually what I was planning to do (thanks for the
link though, 
it should be useful). But to rephrase the problem: I'd like
to have 
different implementations for some methods of different
instances of a 
certain model class.

I guess I'll just forget about attempting to do some sort of
polymorphism 
and just concentrate on the aforementioned class methods.
Maybe I'll just 
check the object id in each such method, and if it is a
special predefined 
id, run some predefined method instead of the default code.
Not very 
pretty but should suffice. Something like:


class Scale(models.Model):
 	# ...
 	def calcGrade1(...):
 		if self.id == 1:
 			return specialScale1CalcGrade1(...)
 		elif self.id == 2:
 			return specialScale2CalcGrade1(...)
 		else:
 			# do the usual stuff
 	# ...


> If you want to restrict the content administrators from
modifying
> those records, you can use Django per-object-permission
branch for
> that.

Yep, I guess I need to do that.

> Hope, this helps you.

Sure, thanks. Knowing something can not be done is just as
valuable 
information as knowing how to do something.


Mikko

--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Hard coded instances of model classes
user name
2006-11-22 16:38:28
I think, you could also override the manager of the Scale
method to
change the behavior of Scale.objects.get(). You should do
something
like this:

class ScaleManager(models.Manager):
    def get(self, *args, **kwargs):
        # let's say that the first ten objects has to be of
the
SpecialScale1 model
        if kwargs['id'] in range(1, 11):
            return SpecialScale1.objects.get(*args,
**kwargs)
        else:
            return self.get_query_set().get(*args, **kwargs)

class Scale(models.Model):
    # ...
    objects = ScaleManager()
    # ...

You will have to ensure somehow, that the ids from 1 to 10
are not
used for the instances of the Scale model.

Good luck!
Aidas Bendoraitis [aka Archatas]

--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Hard coded instances of model classes
user name
2006-11-23 11:27:49
On Wed, 22 Nov 2006, Aidas Bendoraitis wrote:
> I think, you could also override the manager of the
Scale method to
> change the behavior of Scale.objects.get(). You should
do something
> like this:

Oh, this manager stuff was something I was looking for but
somehow managed 
to miss it in the documentation. Thanks for the tip!

Getting an instance of a special scale class via
Scale.objects.get() is of 
course useful but I definitely need to be able to get the
same thing via 
e.g. Parameter.objects.get(id=1).scale. The simplest way for
achieving 
this would seem to be to specify a custom get_query_set()
method. Since 
inheritance is not an option but I still need to have to
have the special 
scales act as the regular ones (excluding the special parts,
of course) 
I'll just replace the default methods in an scale instance
with some 
special methods.

Ok, sounds a bit weird but let me demonstrate it:


# --- clip ---

class ScaleManager(models.Manager):
 	def get_query_set(self):
 		# Get query set.
 		qs = super(ScaleManager, self).get_query_set()
         	for o in qs:
             	if o.specialScale:
 			# Replace the default methods with some special methods.
                 	o.calcDropParam =
attach_method_to_instance(o, specialScale1CDP)
 			# ...
         	return qs

class Scale(models.Model):
 	objects = ScaleManager()
 	def calcDropParam(self, value):
 		# Do something.
 	# ...
 	specialScale = models.BooleanField(editable=False)
 	# ...

def attach_method_to_instance(instance,func):
 	def attached_method(*args,**kwargs):
 		return func(instance,*args,**kwargs)
 	return attached_method

def specialScale1CDP(self, value):
 	# Do something special.

# --- clip ---


So, actually a special scale is just a regular Scale
instance with some 
methods replaced!

I tested this in a python shell and it seems to work ok.
Well, the 
solution is not very elegant but it might be the best I can
get ATM. Maybe 
I'll write some class or a model for taking care of
assigning all the 
special methods to make the solution a bit more pleasant.

I tend to get worried when solutions start to get less and
less elegant 
but I guess I'll just to live with that.


Mikko

--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Hard coded instances of model classes
user name
2006-11-23 12:20:49
Pretty interesting stuff, although I haven't decided yet
where I could
reuse it 

Good luck with other solutions.
Aidas Bendoraitis [aka Archatas]



On 11/23/06, Mikko Suniala <mikko.sunialatut.fi> wrote:
>
> On Wed, 22 Nov 2006, Aidas Bendoraitis wrote:
> > I think, you could also override the manager of
the Scale method to
> > change the behavior of Scale.objects.get(). You
should do something
> > like this:
>
> Oh, this manager stuff was something I was looking for
but somehow managed
> to miss it in the documentation. Thanks for the tip!
>
> Getting an instance of a special scale class via
Scale.objects.get() is of
> course useful but I definitely need to be able to get
the same thing via
> e.g. Parameter.objects.get(id=1).scale. The simplest
way for achieving
> this would seem to be to specify a custom
get_query_set() method. Since
> inheritance is not an option but I still need to have
to have the special
> scales act as the regular ones (excluding the special
parts, of course)
> I'll just replace the default methods in an scale
instance with some
> special methods.
>
> Ok, sounds a bit weird but let me demonstrate it:
>
>
> # --- clip ---
>
> class ScaleManager(models.Manager):
>         def get_query_set(self):
>                 # Get query set.
>                 qs = super(ScaleManager,
self).get_query_set()
>                 for o in qs:
>                 if o.specialScale:
>                         # Replace the default methods
with some special methods.
>                         o.calcDropParam =
attach_method_to_instance(o, specialScale1CDP)
>                         # ...
>                 return qs
>
> class Scale(models.Model):
>         objects = ScaleManager()
>         def calcDropParam(self, value):
>                 # Do something.
>         # ...
>         specialScale =
models.BooleanField(editable=False)
>         # ...
>
> def attach_method_to_instance(instance,func):
>         def attached_method(*args,**kwargs):
>                 return func(instance,*args,**kwargs)
>         return attached_method
>
> def specialScale1CDP(self, value):
>         # Do something special.
>
> # --- clip ---
>
>
> So, actually a special scale is just a regular Scale
instance with some
> methods replaced!
>
> I tested this in a python shell and it seems to work
ok. Well, the
> solution is not very elegant but it might be the best I
can get ATM. Maybe
> I'll write some class or a model for taking care of
assigning all the
> special methods to make the solution a bit more
pleasant.
>
> I tend to get worried when solutions start to get less
and less elegant
> but I guess I'll just to live with that.
>
>
> Mikko
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

[1-6]

about | contact  Other archives ( Real Estate discussion Medical topics )