Re: Get child model class

2011-05-06 Thread Shawn Milochik

This might work:

http://docs.python.org/library/functions.html#isinstance


--
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: Get child model class

2011-05-06 Thread pbzRPA
I now the isinstance function but that will return the CF class. I
want to know the child class of CF for that specific primary key.


On May 6, 9:30 pm, Shawn Milochik  wrote:
> This might work:
>
> http://docs.python.org/library/functions.html#isinstance

-- 
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: Get child model class

2011-05-06 Thread Shawn Milochik

On 05/06/2011 03:32 PM, pbzRPA wrote:

I now the isinstance function but that will return the CF class. I
want to know the child class of CF for that specific primary key.



The isinstance function doesn't return a class. It returns a boolean, 
based on the class(es) you are inquiring about.


You could also try these:

instance._meta.module_name

instance._meta.db_table

These only return strings, but they'll get you the info you're looking for.


--
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: Get child model class

2011-05-06 Thread pbzRPA
Just to clarify your suggestion

In [100]: isinstance(x, CFT)
Out[100]: False

In [101]: isinstance(x, CFI)
Out[101]: False

In [105]: isinstance(e, CF)
Out[105]: True

This is not what I am looking for.

I want to know to what class the CF primary key points to. So if it's
CFT or CFI

-- 
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: Get child model class

2011-05-06 Thread Ethan Jucovy
Is your CF base class an abstract base class[1]?  If it's not, then your
database actually has separate tables for CF, CFI and CFT .. and when you
access a `doc.cf` you're really getting the CF model instance, *not* the
derived class instance.

IIRC if you make CF an abstract base class then this will work the way you
want -- accessing doc.cf will return an instance of the appropriate derived
class.

Alternatively, if you want to keep the multi-table setup, you can try to get
each derived class instance from the base class, as described here[2] ("If
you have a Place that is also a Restaurant...") -- like:

{{{
x = doc.cf.all()[0]
try:
  x = x.cfi
  x_is_a_CFI = True
  x_is_a_CFT = False
except CFI.DoesNotExist:
  try:
x = x.cft
x_is_a_CFT = True
x_is_a_CFI = False
  except CFT.DoesNotExist:
# x is neither a CFI nor a CFT
}}}

[1]
http://docs.djangoproject.com/en/dev/topics/db/models/#abstract-base-classes

[2]
http://docs.djangoproject.com/en/dev/topics/db/models/#multi-table-inheritance

On Fri, May 6, 2011 at 3:41 PM, pbzRPA  wrote:

> Just to clarify your suggestion
>
> In [100]: isinstance(x, CFT)
> Out[100]: False
>
> In [101]: isinstance(x, CFI)
> Out[101]: False
>
> In [105]: isinstance(e, CF)
> Out[105]: True
>
> This is not what I am looking for.
>
> I want to know to what class the CF primary key points to. So if it's
> CFT or CFI
>
> --
> 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.
>
>

-- 
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: Get child model class

2011-05-07 Thread pbzRPA
Thank you, this is more or less what I am looking for. I don't however
want to make the CF class an abstract class as I want to have some
common fields in there. The Idea behind this is that I want to store
custom fields (CF) for various models that a user will be defining
themselves. I do not want to keep the data in a single table as I want
to be able to filter and do database actions on specific field types.
But in order recreate a dynamic form from all the CF fields belonging
to a document (Doc)  I want to be able to iterate over Doc.cf.all()
and add the specific CF field type to a form. I hope that makes
sense :)


-- 
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: Get child model class

2011-05-07 Thread pbzRPA
this is more or less what I found. I will have to see how I can
implement it on my side.

http://djangosnippets.org/snippets/1037/

-- 
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: Get child model class

2011-05-07 Thread pbzRPA
Ok this solution worked for me :):)

{{{
from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.db.models.query import QuerySet

class SubclassingQuerySet(QuerySet):

def __getitem__(self, k):
result = super(SubclassingQuerySet, self).__getitem__(k)
if isinstance(result, models.Model) :
return result.as_leaf_class()
else :
return result
def __iter__(self):
for item in super(SubclassingQuerySet, self).__iter__():
yield item.as_leaf_class()

class CFManager(models.Manager):
def get_query_set(self):
return SubclassingQuerySet(self.model)

class CF(models.Model):
type = models.CharField(max_length = 40)
content_type =
models.ForeignKey(ContentType,editable=False,null=True)
objects = CFManager()

def save(self, *args, **kwargs):
if(not self.content_type):
self.content_type =
ContentType.objects.get_for_model(self.__class__)
super(CF, self).save(*args, **kwargs)

def as_leaf_class(self):
content_type = self.content_type
model = content_type.model_class()
if (model == CF):
return self
return model.objects.get(id=self.id)

class CFT(CF):
data = models.CharField(max_length=20)
objects = CFManager()

class CFI(CF):
data = models.IntegerField()
objects = CFManager()

class Doc(models.Model):
name = models.CharField(max_length = 40)
cf = models.ManyToManyField(CF, null = True)
}}}

now when I do:

In [12]: doc.cf.all()
Out[12]: [, ]

Thanks for all the suggestions and help.

-- 
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.