Hey Thanks

You were right here is what I ended up doing to get it working..

for item in p4.run(("describe", 122)):
  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()

I wonder if there is a more elegant way to do this??  What happens
when the client exist - Well I get a Client.DoesNotExist of course..
Hmmm.  This forced me to do this..

def getOrCreateClient(client):
  try:
    cl = Client.objects.get(client=client)
  except Client.DoesNotExist:
    cl = Client()
    cl.client = c.get("Client")
  cl.access         = datetime.datetime.strptime(c['Access'], "%Y/%m/
%d %H:%M:%S")
  cl.description    = c.get("Description", "")
  cl.host           = c.get("Host", "")
  cl.lineEnd        = c.get("LineEnd")
  cl.options        = c.get("Options", "")
  cl.owner          = c.get("Owner")
  cl.root           = c.get("Root")
  cl.submitOptions  = c.get("SubmitOptions")
  cl.update         = datetime.datetime.strptime(c['Update'], "%Y/%m/
%d %H:%M:%S")
  cl.save()
  return cl

for item in p4.run(("describe", 122)):
  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(getOrCreateClient(item['client']))
  entry.save()

A bit better but still could be cleaner..  Thanks for helping!

On Aug 15, 5:39 am, Léon Dignòn <leon.dig...@gmail.com> wrote:
> Hello rh0dium,
>
> Client.objects.filter() returns you a QuerySet instance which you have
> to iterate to get all objects. Thus the commented line will generate
> an error, I think so.
>
> Instead you should use Client.objects.get() to get the model instance.
> A QuerySet is a list of model instances.
>
> > Can anyone show me where the error of my ways is.  I would really
> > appreciate it.
>
> Please post the error message with the full source code next time. :)
>
> Léon
>
> On Aug 15, 3:15 am, rh0dium <steven.kl...@gmail.com> wrote:
>
>
>
> > Hi Guys,
>
> > First post to the group 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..
>
> > --- models.py ---
> > 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()
> >   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()
>
> > ---  massImport.py ---
>
> > Can anyone show me where the error of my ways is.  I would really
> > appreciate it.
>
> > 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-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