models.py with ManyToMany and progmatically adding data via a shell script
Question: First post to stackoverflow I did do a search and came up dry. I also own the
django book (Forcier,Bissex,Chun) and they don't explain how to do this. In
short I can't figure out how to progmatically add a data via a python shell
script to the ManyToMay model..

    
    
    from django.db import models
    from django.contrib import admin
    
    class Client(models.Model):
      client        = models.CharField(max_length=256, primary_key=True)
      access        = models.DateField()
      description   = models.TextField()
      host          = models.CharField(max_length=256)
      lineEnd       = models.CharField(max_length=256)
      options       = models.TextField()
      owner         = models.CharField(max_length=100)
      root          = models.CharField(max_length=256)
      submitOptions = models.CharField(max_length=256)
      update        = models.DateField()
      def __unicode__(self):
        return str(self.client)
    admin.site.register(Client)
    
    
    class Change(models.Model):
      """This simply expands out 'p4 describe' """
      change        = models.IntegerField(primary_key=True)
      client        = models.ManyToManyField(Client)
      desc          = models.TextField()
      status        = models.CharField(max_length=128)
      def __unicode__(self):
        return str(self.change)
    admin.site.register(Change)
    

Here is what I have which works but I don't know how to add the ManyToMany. I
can't seem to figure out how to progmatically call it. I know the row in SQL
exists.

\--- massImport.py ---

    
    
    # Assume the client "clientspec" exists.  I know how to create that if 
    neeeded. 
    changes = [ { 'change': 123, 'desc': "foobar", status': "foobar", 
    client': "clientspec", }] 
    for item in changes: 
      entry = Change( 
                change    = item['change'], 
                desc    = item['desc'], 
                status    = item['status'], 
                # client    = Client.objects.filter(client=item['client']) 
                ) 
      entry.save()
    

Can anyone show me where the error of my ways is. I would really appreciate
it. Thanks!!


Answer: Turns out Tiago was very close..

    
    
    # Assume the client "clientspec" exists.  I know how to create that if 
    neeeded. 
    changes = [ { 'change': 123, 'desc': "foobar", status': "foobar", 
    client': "clientspec", }] 
    for item in changes: 
      entry = Change()
      entry.change  = item['change']
      entry.desc    = item['desc']
      entry.status  = item['status']
      entry.time    = datetime.datetime.fromtimestamp(float(item['time']))
      entry.client.add(Client.objects.get(client=item['client']))
      entry.save()
    

So.. I will give props to Tiago

