On Wed, 2009-06-17 at 12:55 -0500, Dan Sheffner wrote: > I have this in my model: > > class Machine(models.Model): > name = models.CharField(max_length=100) > cpuInfo = models.ForeignKey(Cpu, blank=True, null=True) > > class Cpu(models.Model): > l = models.CharField(max_length=50) > num = models.IntegerField() > > so basically I add machines to the Machine table and then loop through > each and find out the processor type and num. I want to insert and > attach it to the machine but I'm confused on how to do this? Can > someone show me the syntax? > > procType = 'intel' > numProcessor = 4 > > for e in Machine.objects.all(): > print e.name > >
Your question is a little unclear. Do your machines already have cpus, and you want to find out the information, or do you want to create a relationship between your machines and your cpus? One of the following *might* meet your needs. To get the cpu information for each processor: >>> for machine in Machine.objects.all(): ... print machine.cpuInfo.l # l is a bad name. ... print machine.cpuInfo.num Or to set a processor for each machine: >>> for machine in Machine.objects.all(): ... processor, is_new = Cpu.objects.get_or_create( ... l=procType, ... num=numProcessor ... ) ... machine.cpuInfo = processor ... machine.save() > > > > > > > > > > > > > > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---