Override Django ORM with virtual model class - is it possible or no?

1,287 views
Skip to first unread message

test157

unread,
Jul 6, 2010, 2:43:39 PM7/6/10
to Django users
hello,

I need one solution so hope you can help me, e.g. I have this model

class GenericModel(models.Model):
val1 = models.CharField(max_length=50)
val2 = models.CharField(max_length=80)

and I need to create many virtual models based on this one above, so
it will looks this way:

class Info(models.Model):
name = GenricModel.val1
address = GenericModel.val2

class Details(models.Model):
size = GenricModel.val1
height = GenericModel.val2

e.t.c.

is it possible or no? so when I access Details or Info I have to work
with GenericModel - so it's some kind of proxy, but with overrided
field names.

anyone knows how to do it in Django? or it's impossible?

thusjanthan

unread,
Jul 6, 2010, 5:29:41 PM7/6/10
to Django users
Hi,

I am not sure if what your asking is possible as a field type in a
database is generally a single field. What you are trying to do is
have an object as a field type. Perhaps you can do a ForeignKey and
have a m2m relationship between the GenericModel? From what I remember
you can have custom field types but it can only be types from a
database. Don't quote me on that but I think so. Have a look at this
where they go further into custom model fields.

http://docs.djangoproject.com/en/dev/howto/custom-model-fields/#custom-database-types

Hope this helps,

Thusjanthan Kubendranathan.

test157

unread,
Jul 6, 2010, 5:52:05 PM7/6/10
to Django users
ok, I'll try to explain once again. I have real model

class GenericModel(models.Model):
val1 = models.CharField(max_length=50)
val2 = models.CharField(max_length=80)

it has its table in database like generic_model with fields like
id | val1 | val2

the idea is just to create one this table and access this table by
using other "virtual models", which will work like proxies for
GenericModel.

so I will create just "proxy" model

class Info(models.Model):
name = GenricModel.val1
address = GenericModel.val2

what will has also 2 fields what are proxied to Generic Model fields.

so when I access Info.objects.filter(name='John') in real underneath
it has to access GenericModel.objects.filter(val1='John')

hope you can understand now
Reply all
Reply to author
Forward
0 new messages