Is there a way to dumpdata truncate to last n lines?
Hi Folks, Is there anyway to dump the last 'n' lines of the db. I like to build test fixtures but with 3M lines it's a bit much... Thanks --- Steven M. Klass ☎ 1 (480) 225-1112 ✉ skl...@pointcircle.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 does my model look?
Hi Guys, Relative newbie and I want to ensure I'm getting started on the right track. How does this model look? # encoding: utf-8 """ class P4Document(models.Model): depotfile = models.CharField(max_length=1024) action= models.CharField(choices=(("add","Add"), ("edit", "Edit"), ("delete", "Delete"), ("integrate", "Integrate")), max_length=15 ) digest= models.CharField(max_length=128) rev = models.IntegerField() type = models.CharField(choices=(("text", "Text file"), ("xtext", "Executable text file"), ("ktext", "Text file with RCS keyword expansion"), ("kxtext", "Executable text file with RCS keyword expansion"), ("binary", "Non-text file"), ("xbinary", "Executable binary file"), ("ctext", "Compressed text file"), ("cxtext", "Compressed executable text file"), ("symlink", "Symbolic link"), ("resource", "Macintosh resource fork"), ("uresource", "Uncompressed Macintosh resource fork"), ("ltext", "Long text file"), ("xltext", "Executable long text file"), ("ubinary", "Uncompressed binary file"), ("uxbinary", "Uncompressed executable binary file"), ("tempobj", "Temporary object"), ("ctempobj", "Temporary object (compressed)"), ("xtempobj", "Temporary executable object"), ("xunicode", "Executable unicode")), max_length=15 ) def __unicode__(self): return str(self.depotfile) class P4Change(models.Model): """This simply expands out 'p4 describe' """ describe = models.IntegerField(primary_key=True) change= models.IntegerField() client= models.CharField(max_length=128) user = models.CharField(max_length=128) depotfiles= models.ManyToManyField(P4Document) desc = models.TextField() status= models.CharField(max_length=128) time = models.DateField(auto_now_add=True) objects = P4ChangeManager() def __unicode__(self): return str(self.change) # Sample Data.. change = {'change': '1', 'client': 'deschutes', 'desc': 'Matlab scripts used in production\r\n', 'status': 'submitted', 'time': '938545789', 'action': ['add', 'add', 'add'], 'depotFile': ['//depot/projects/Matlab/Points.m', '//depot/projects/Matlab/contents.m', '//depot/projects/Matlab/sigcheck.m'], 'digest': ['5842D2BB334D3BF2741BB2C8F5B1AE03', '33E2EC8611003D6D9321FF3E9005C627', 'DB4C48FE0A94F3D4FCFE57A63EE3EFFE'], 'rev': ['1', '1', '1'], 'type': ['text', 'text', 'text'], 'user': 'mtr'} Questions... 1) Does this look reasonable? Is this the right way to structure my model. I will have lots (millions) of changes and 10's of millions of documents. Are there better ways to structure my code? --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Models.py and overriding the get_or_create class method. Help please with obj = self.model(**params)
Hi all, I have a class in which I want to override the get_or_create method. Basically if my class doesn't store the answer I want it do some process to get the answer and it's not provided. The method is really a get_or_retrieve method. I have heavily borrowed this from db/models/ query.py and I can't figure out the line "obj = self.model(**params)". I don't understand what the attr model needs to be and it's not intuitively obvious what this should be. Even looking back at the query.py I can't figure this out. Can someone explain this to me? I would really like to understand it and fix my code. So here's the class: class P4User(models.Model): user = models.CharField(max_length=100, primary_key=True) fullname = models.CharField(max_length=256) email = models.EmailField() access= models.DateField(auto_now_add=True) update= models.DateField(auto_now_add=True) @classmethod def get_or_retrieve(self, username, auto_now_add=False): try: return self.get(user=username), False except self.model.DoesNotExist: import P4 import datetime from django.db import connection, transaction, IntegrityError p4 = P4.P4().connect() kwargs = p4.run(("user", "-o", username))[0] p4.disconnect() params = dict( [(k.lower(),v) for k, v in kwargs.items ()]) obj = self.model(**params) sid = transaction.savepoint() obj.save(force_insert=True) transaction.savepoint_commit(sid) return obj, True except IntegrityError, e: transaction.savepoint_rollback(sid) try: return self.get(**kwargs), False except self.model.DoesNotExist: raise e def __unicode__(self): return str(self.user) I am able to get the params but I haven't defined self.model. I don't understand what that needs to be and it's not intuitively obvious what value that should be. Even looking back at the query.py I can't figure this out. Can someone explain this to me? I would really like to understand it and fix my code. 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 -~--~~~~--~~--~--~---
Re: ManyToMany Model.py and the shell..
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 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 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 -~--~~~~--~~--~--~---
ManyToMany Model.py and the shell..
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 -~--~~~~--~~--~--~---