Re: Date from DateTimeField vs date from model method: foreign key DateTimeField
Gabriel [SGT] wrote: On Fri, Apr 12, 2013 at 2:47 PM, Mike Doroshenko II wrote: In my attached models.py Please, don't attach code, use a paste service or similar to view the code online. Ok, yeah I suppose that would save storage space and bandwidth considering the fact I have a few copied of the same email (From my sent folder and I'm getting some emails twice). the time last modified on the latest object that has a foreign key to it. So when I made a new document, in the admin interface I see April 11, 2013, 8:24 p.m. for the Created column and 2013-04-11 20:24:36.799167 for the Last Modified column. Also I can click to sort the results by Created date, but not by last modified date. I'm afraid any reason why it's different. Are you using your own ModelAdmin ? class DocAdmin(admin.ModelAdmin): list_display = ['name','created','last_modified'] Additionally in the same file in the model method I am using a for-loop to get the value for Last Modified. I imagine going through each object just to pull the data from the last one isn't very efficient and pretty redundant. I tried to just pull the last object directly but am getting an error: I modified your code accordingly http://pastebin.com/7NisAVfN be aware that if a 'Piece' matching that criteria doesn't exists, it will raise a DoesNotExist exception, maybe you'll want to catch it. psst ->> https://docs.djangoproject.com/en/dev/ref/models/querysets/ Hmm so it was caused by a Document with no pieces. The code you provided simply returned None for that one :) Thanks, Mike -- Mike Doroshenko, Junior Sys Admin TecKnoQuest Inc. mi...@tecknoquest.com www.tecknoquest.com -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Re: Date from DateTimeField vs date from model method: foreign key DateTimeField
On Fri, Apr 12, 2013 at 2:47 PM, Mike Doroshenko II wrote: > In my attached models.py Please, don't attach code, use a paste service or similar to view the code online. > the time last modified on the latest object that has a foreign key to it. > > So when I made a new document, in the admin interface I see April 11, 2013, > 8:24 p.m. for the Created column and 2013-04-11 20:24:36.799167 for the Last > Modified column. > > Also I can click to sort the results by Created date, but not by last > modified date. I'm afraid any reason why it's different. Are you using your own ModelAdmin ? > > Additionally in the same file in the model method I am using a for-loop to > get the value for Last Modified. I imagine going through each object just to > pull the data from the last one isn't very efficient and pretty redundant. I > tried to just pull the last object directly but am getting an error: I modified your code accordingly http://pastebin.com/7NisAVfN be aware that if a 'Piece' matching that criteria doesn't exists, it will raise a DoesNotExist exception, maybe you'll want to catch it. psst ->> https://docs.djangoproject.com/en/dev/ref/models/querysets/ > -- > Mike Doroshenko, Junior Sys Admin Regards -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Re: Date from DateTimeField vs date from model method: foreign key DateTimeField
Hello? Mike Doroshenko II wrote: In my attached models.py I have a class that has a model method to return the time last modified on the latest object that has a foreign key to it. So when I made a new document, in the admin interface I see April 11, 2013, 8:24 p.m. for the Created column and 2013-04-11 20:24:36.799167 for the Last Modified column. Also I can click to sort the results by Created date, but not by last modified date. How can I sanitize the Last Modified field so that it appears like Created does and I can sort by it? Additionally in the same file in the model method I am using a for-loop to get the value for Last Modified. I imagine going through each object just to pull the data from the last one isn't very efficient and pretty redundant. I tried to just pull the last object directly but am getting an error: TemplateSyntaxError at /admin/grace/document/ Caught IndexError while rendering: list index out of range -- Mike Doroshenko, Junior Sys Admin TecKnoQuest Inc. mi...@tecknoquest.com www.tecknoquest.com -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out. -- Mike Doroshenko, Junior Sys Admin TecKnoQuest Inc. mi...@tecknoquest.com www.tecknoquest.com -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out. from django.db import models import datetime class Document(models.Model): name = models.CharField(max_length=22) created = models.DateTimeField(auto_now_add=True) def __unicode__(self): return self.name def last_modified(self): for m in self.piece_set.all(): global x x = m.last_modified date = x #obj = self.piece_set.order_by("-id")[0] #date = obj.last_modified # Causes IndexError when rendering template in admin interface. return date class Piece(models.Model): sort_int = models.IntegerField() doc = models.ForeignKey(Document) text = models.TextField() last_modified = models.DateTimeField(auto_now=True) def __unicode__(self): return "%s: %d" % (self.doc.name, self.sort_int)
Date from DateTimeField vs date from model method: foreign key DateTimeField
In my attached models.py I have a class that has a model method to return the time last modified on the latest object that has a foreign key to it. So when I made a new document, in the admin interface I see April 11, 2013, 8:24 p.m. for the Created column and 2013-04-11 20:24:36.799167 for the Last Modified column. Also I can click to sort the results by Created date, but not by last modified date. How can I sanitize the Last Modified field so that it appears like Created does and I can sort by it? Additionally in the same file in the model method I am using a for-loop to get the value for Last Modified. I imagine going through each object just to pull the data from the last one isn't very efficient and pretty redundant. I tried to just pull the last object directly but am getting an error: TemplateSyntaxError at /admin/grace/document/ Caught IndexError while rendering: list index out of range -- Mike Doroshenko, Junior Sys Admin TecKnoQuest Inc. mi...@tecknoquest.com www.tecknoquest.com -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out. from django.db import models import datetime class Document(models.Model): name = models.CharField(max_length=22) created = models.DateTimeField(auto_now_add=True) def __unicode__(self): return self.name def last_modified(self): for m in self.piece_set.all(): global x x = m.last_modified date = x #obj = self.piece_set.order_by("-id")[0] #date = obj.last_modified # Causes IndexError when rendering template in admin interface. return date class Piece(models.Model): sort_int = models.IntegerField() doc = models.ForeignKey(Document) text = models.TextField() last_modified = models.DateTimeField(auto_now=True) def __unicode__(self): return "%s: %d" % (self.doc.name, self.sort_int)