On Thu, 2009-07-16 at 08:30 -0700, mike wrote:
> Hi,
> 
> I have the following models:
> 
> class Memory(models.Model):
>     partnum = models.CharField(max_length=30)
>     size = models.IntegerField()
> 
>     def __unicode__(self):
>         return self.partnum
> 
> class Motherboard(models.Model):
>     name = models.CharField(max_length=20)
>     sockets = models.IntegerField()
>     modules = models.ManyToManyField(Memory, through='MemModules')
> 
>     def __unicode__(self):
>         return self.name
> 
> class MemModules(models.Model):
>    memory = models.ForeignKey(Memory)
>    motherboard = models.ForeignKey(Motherboard)
>    count = models.IntegerField()
> 
> I can use the following statements to access the memory items for a
> particular motherboard:
> 
> >>> from mike.test.models import *
> >>> m = Motherboard.objects.get(name="C7X58")
> >>> print m.name
> C7X58
> >>> print m.modules
> <django.db.models.fields.related.ManyRelatedManager object at
> 0x9763f6c>
> >>> mods = m.modules.all()
> >>> print mods
> [<Memory: ACT1GHU64B8F1333S>, <Memory: ACT2GHU64B8F1333S>]
> >>> for i in mods:
> ...    print i.partnum
> ...
> ACT1GHU64B8F1333S
> ACT2GHU64B8F1333S
> 
> My question is:  How do I access the extra fields stored in my custom
> intermediate table?  In this case that would be the count field.  I
> have search the Django documentation but so far I haven't found any
> examples.
> 
> Thanks for any help!
> 
> Mike
> 

m = Motherboard.objects.get(name="C7X58")
mem_mods = m.memmodules_set.all()
for mem_mod in mem_mods:
  print "%d x %s" % (mem_mod.count, mem_mod.memory) 

Cheers

Tom


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

Reply via email to