Re: Increment a value in a database table field
Exactly Karen the model is confusing. Here is the exactly model. class Resource_Track(models.Model) users = models.ForeignKey(User) resources = models.ForeignKey(Resource) date_tracked = models.DateTimeField('Date Tracked') description = models.TextField() status_count_per_week = models.IntegerField() status = models.ForeignKey(Status) class Meta: verbose_name_plural = 'Track Resources' Sorry for confusing you. Regards Denis. On Sat, Nov 14, 2009 at 7:43 AM, Karen Tracey wrote: > On Sat, Nov 14, 2009 at 10:25 AM, Denis Bahati wrote: > >> Hi, >> Here is my model am using to update items status. >> >> class User(models.Model) >> users = models.ForeignKey(User) >> resources = models.ForeignKey(Resource) >>date_tracked = models.DateTimeField('Date Tracked') >>description = models.TextField() >>status_count_per_week = models.IntegerField() >> status = models.ForeignKey(Status) >> >> class Meta: >> verbose_name_plural = 'Track Resources' >> >> What i want to achieve is that: >> When i update the status the item first i should get the date last >> tracked and compare with tje current date, if the date tracked and >> the current date are not in the same week it should insert into a new row of >> status_count_per_week. If the date tracked and the current date are in the >> same week it should increment the value of status_count_per_week . >> >> > I find your model and description confusing -- I am not sure the model you > have defined is correct for what you want to achieve -- so I am not going to > attempt to craft a solution exactly. Instead I'll point you to the two > building blocks I think you need to achieve what you are looking for. > First, get_or_create: > > > http://docs.djangoproject.com/en/dev/ref/models/querysets/#get-or-create-kwargs > > which you can use to either get the model containing a specific set of > fields or create it if it does not exist. > > Second, update() with an F() expression can be used to atomically increment > a counter: > > > http://docs.djangoproject.com/en/dev/ref/models/instances/#updating-attributes-based-on-existing-fields > > Karen > > > > -- > 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=. > -- 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=.
Re: Increment a value in a database table field
On Sat, Nov 14, 2009 at 10:25 AM, Denis Bahati wrote: > Hi, > Here is my model am using to update items status. > > class User(models.Model) > users = models.ForeignKey(User) > resources = models.ForeignKey(Resource) >date_tracked = models.DateTimeField('Date Tracked') >description = models.TextField() >status_count_per_week = models.IntegerField() > status = models.ForeignKey(Status) > > class Meta: > verbose_name_plural = 'Track Resources' > > What i want to achieve is that: > When i update the status the item first i should get the date last > tracked and compare with tje current date, if the date tracked and > the current date are not in the same week it should insert into a new row of > status_count_per_week. If the date tracked and the current date are in the > same week it should increment the value of status_count_per_week . > > I find your model and description confusing -- I am not sure the model you have defined is correct for what you want to achieve -- so I am not going to attempt to craft a solution exactly. Instead I'll point you to the two building blocks I think you need to achieve what you are looking for. First, get_or_create: http://docs.djangoproject.com/en/dev/ref/models/querysets/#get-or-create-kwargs which you can use to either get the model containing a specific set of fields or create it if it does not exist. Second, update() with an F() expression can be used to atomically increment a counter: http://docs.djangoproject.com/en/dev/ref/models/instances/#updating-attributes-based-on-existing-fields Karen -- 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=.
Re: Increment a value in a database table field
Hi, Here is my model am using to update items status. class User(models.Model) users = models.ForeignKey(User) resources = models.ForeignKey(Resource) date_tracked = models.DateTimeField('Date Tracked') description = models.TextField() status_count_per_week = models.IntegerField() status = models.ForeignKey(Status) class Meta: verbose_name_plural = 'Track Resources' What i want to achieve is that: When i update the status the item first i should get the date last tracked and compare with tje current date, if the date tracked and the current date are not in the same week it should insert into a new row of status_count_per_week. If the date tracked and the current date are in the same week it should increment the value of status_count_per_week . Regards. Denis. On Thu, Nov 12, 2009 at 11:58 AM, scot.hac...@gmail.com < scot.hac...@gmail.com> wrote: > > On Nov 12, 7:51 am, Denis Bahati wrote: > > Hi All, > > > > I have a field with an integer value which needs to be incremented > > every time any user updates its status within a week. If that week has > > passed it should insert a new row. Now i was trying to figure out but > > didn't get any concept on how to go through. Please any idea? > > You don't mention how you're tracking dates or weeks (is that a > related model, or... ?) but this would be the general idea: > > - get the timestamp of the last update to that field and compare it to > the current timestamp > > if ... : # interval is less than a week: >foo = MyModel.objects.get(...) >foo.counter = foo.counter + 1 > else : interval is more than a week >foo = MyModel() >foo.counter = 1 > > foo.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-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=. > > > -- 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=.
Re: Increment a value in a database table field
On Nov 12, 7:51 am, Denis Bahati wrote: > Hi All, > > I have a field with an integer value which needs to be incremented > every time any user updates its status within a week. If that week has > passed it should insert a new row. Now i was trying to figure out but > didn't get any concept on how to go through. Please any idea? You don't mention how you're tracking dates or weeks (is that a related model, or... ?) but this would be the general idea: - get the timestamp of the last update to that field and compare it to the current timestamp if ... : # interval is less than a week: foo = MyModel.objects.get(...) foo.counter = foo.counter + 1 else : interval is more than a week foo = MyModel() foo.counter = 1 foo.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-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=.
Increment a value in a database table field
Hi All, I have a field with an integer value which needs to be incremented every time any user updates its status within a week. If that week has passed it should insert a new row. Now i was trying to figure out but didn't get any concept on how to go through. Please any idea? -- 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=.