Re: How can I access attributes in intermediate class

2010-07-01 Thread cat in a tub
I found model inheritance may be a better solution: combine may model
into one (Similar to views combining tables in database)


Quote:

Model inheritance in Django works almost identically to the way normal
class inheritance works in Python. The only decision you have to make
is whether you want the parent models to be models in their own right
(with their own database tables), or if the parents are just holders
of common information that will only be visible through the child
models.

There are three styles of inheritance that are possible in Django.

   1. Often, you will just want to use the parent class to hold
information that you don't want to have to type out for each child
model. This class isn't going to ever be used in isolation, so
Abstract base classes are what you're after.
   2. If you're subclassing an existing model (perhaps something from
another application entirely) and want each model to have its own
database table, Multi-table inheritance is the way to go.
   3. Finally, if you only want to modify the Python-level behaviour
of a model, without changing the models fields in any way, you can use
Proxy models.


On Jun 29, 12:04 pm, Alexandre González  wrote:
> I was intrigued so I did it, I get this solution:
>
> from models import User, Group, Membership
>
> def example(request):
>     example_group_id = 1 #I create only one group to test, this is his id
>
>     group = Group.objects.get(pk=example_group_id)
>     for user in group.members.iterator(): #Get all user in the group
> throught the membership
>         membership = Membership.objects.get(person=user, group=group) #Get
> the membership object using the user and the group as keys
>         membership.date_joined # Now you can read your date_joined
>
> Perhaps it isn't the best way to do it, but it works :p
>
> I hope this helps you,
> Álex González
>
> On Tue, Jun 29, 2010 at 19:45, cat in a tub  wrote:
>
>
>
> > class User(models.Model):
> >    name = models.CharField(max_length=128)
>
> > class Group(models.Model):
> >    name = models.CharField(max_length=128)
> >    members = models.ManyToManyField(User, through='Membership')
>
> > class Membership(models.Model):
> >    person = models.ForeignKey(User)
> >    group = models.ForeignKey(Group)
> >    date_joined = models.DateField()
>
> > What if I want to access Membership.data_joined in Group class?
>
> > Thanks
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Django users" group.
> > To post to this group, send email to django-us...@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.
>
> --
> Please, don't send me files with extensions: .doc, .docx, .xls, .xlsx, .ppt
> and/or .pptxhttp://mirblu.com

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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: How can I access attributes in intermediate class

2010-06-29 Thread Alexandre González
I was intrigued so I did it, I get this solution:

from models import User, Group, Membership

def example(request):
example_group_id = 1 #I create only one group to test, this is his id

group = Group.objects.get(pk=example_group_id)
for user in group.members.iterator(): #Get all user in the group
throught the membership
membership = Membership.objects.get(person=user, group=group) #Get
the membership object using the user and the group as keys
membership.date_joined # Now you can read your date_joined

Perhaps it isn't the best way to do it, but it works :p

I hope this helps you,
Álex González

On Tue, Jun 29, 2010 at 19:45, cat in a tub  wrote:

> class User(models.Model):
>name = models.CharField(max_length=128)
>
> class Group(models.Model):
>name = models.CharField(max_length=128)
>members = models.ManyToManyField(User, through='Membership')
>
> class Membership(models.Model):
>person = models.ForeignKey(User)
>group = models.ForeignKey(Group)
>date_joined = models.DateField()
>
>
> What if I want to access Membership.data_joined in Group class?
>
> Thanks
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django-us...@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.
>
>


-- 
Please, don't send me files with extensions: .doc, .docx, .xls, .xlsx, .ppt
and/or .pptx
http://mirblu.com

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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.



How can I access attributes in intermediate class

2010-06-29 Thread cat in a tub
class User(models.Model):
name = models.CharField(max_length=128)

class Group(models.Model):
name = models.CharField(max_length=128)
members = models.ManyToManyField(User, through='Membership')

class Membership(models.Model):
person = models.ForeignKey(User)
group = models.ForeignKey(Group)
date_joined = models.DateField()


What if I want to access Membership.data_joined in Group class?

Thanks

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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.