13๐
All answers were useful, but I finally did it this way:
def save(self, *args, **kwargs):
if self.published and self.pub_date is None:
self.pub_date = timezone.now()
elif not self.published and self.pub_date is not None:
self.pub_date = None
super(Model, self).save(*args, **kwargs)
21๐
You want to add the auto_now field and set it to True. This will update with the current timestamp each time you update the model.
pub_date = models.DateTimeField('date_published', auto_now=True)
You can read about it here
Edit
Sorry you really just want to change the timestamp when the value of published is set to True. A really easy way to do this is to get the original value of the model and then override the save method so that it gets updated when it is set to True. Here is what you add to your code:
class MyModel(models.Model):
published = models.BooleanField(default=False)
pub_date = models.DateTimeField('date published')
def __init__(self, *args, **kwargs):
super(MyModel, self).__init__(*args, **kwargs)
self._published = self.published
def save(self, *args, **kwargs):
if not self._published and self.published:
self.pub_date = django.timezone.now()
super(MyModel, self).save(*args, **kwargs)
- [Django]-What is {% block content %} and {% endblock content %} for in Django?
- [Django]-Django 1.5b1: executing django-admin.py causes "No module named settings" error
- [Django]-How do I get the values of all selected checkboxes in a Django request.POST?
11๐
If you are setting the object as published in the Django admin, a nice way to do this is to override the save_model
method of your model admin class.
from datetime import datetime
from django.contrib import admin
class MyModelAdmin(admin.ModelAdmin):
def save_model(self, request, obj, form, change):
if obj.published and 'published' in form.changed_data
obj.pub_date = datetime.now()
obj.save()
admin.site.register(MyModel, MyModelAdmin)
If you are setting the published flag elsewhere, then you can override the save()
method, or use the pre_save
signal. This isnโt as elegant, because itโs harder to tell whether the published
flag has changed. I think you need to re-fetch the object from the database to check.
- [Django]-Filter by property
- [Django]-Django REST Serializer Method Writable Field
- [Django]-How can I temporarily disable a foreign key constraint in MySQL?
9๐
the following only changes your pub_date if published has been modified from False to True:
from datetime import datetime
def __init__(self, *args, **kwargs):
super(MyModel, self).__init__(*args, **kwargs)
self.old_published = self.published
def save(self, *args, **kwargs):
if self.published and self.old_published != self.published:
self.pub_date = datetime.now()
super(Model, self).save(*args, **kwargs)
- [Django]-Show a ManyToManyField as Checkboxes in Django Admin
- [Django]-Django: Increment blog entry view count by one. Is this efficient?
- [Django]-Visual studio code breakpoint set to grey color & not working.error(may be excluded because of "justMyCode" option)
2๐
Create a publish()
method:
def publish(self):
self.published = True
self.pub_date = datetime.datetime.now()
self.save()
- [Django]-Is it possible to decorate include(โฆ) in django urls with login_required?
- [Django]-Django rest framework: set field-level error from serializer validate() method
- [Django]-How do I convert datetime.timedelta to minutes, hours in Python?
0๐
just Override Update method
from django.utils import timezone
class Vehicle(models.Model):
class Meta:
ordering = ['-created']
created = models.DateTimeField(default=timezone.now)
updated = models.DateTimeField(default=timezone.now)
def update(self, *args, **kwargs):
kwargs.update({'updated': timezone.now})
super().update(*args, **kwargs)
return self
- [Django]-How do I display the value of a Django form field in a template?
- [Django]-Django templates: loop through and print all available properties of an object?
- [Django]-Django: Support for string view arguments to url() is deprecated and will be removed in Django 1.10
0๐
A really tricky easy way to do it, is by actually getting the query after you update it, and invoking save()
on it, this all happens in the view, I will create the simplest implementation of a view that receive a post request:
def update_published_view(request,pk):
if request.method == 'POST':
models.MyModel.objects.filter(pk=pk).update(published=request.POST.get('is_published','')
my_model_instance = models.MyModel.objects.get(pk=pk) # the trick here is to access
#the instance after it is updated and then invoke `save()`
#on the query to actually update the date field
my_model_instance.save()
return render(request,'somepage.html')
and donโt forget to set auto_now
to True
in pub_date
field in MyModel
model.
- [Django]-How should I write tests for Forms in Django?
- [Django]-Kombu.exceptions.EncodeError: User is not JSON serializable
- [Django]-Read a local file in django