Django datetime field is not saving

817 views Asked by At

I have this weird issue saving a DateTimeField with Django 1.8 using Mariadb.

In a veiws I‌ have:

        print 'going to update lastposted', topic.lastposted
        topic.lastposted = timezone.now()
        topic.save()
        print 'new topic lastposted time', topic.lastposted

the field lastposted is defined in the model as:

class Topic(models.Model):
    #other fields
    lastposted = models.DateTimeField(blank=True)

    def save(self, *args, **kwargs):
        if not self.id and not self.slug:
            slug = slugify(self.title)
            slug_exists = True
            counter = 1
            self.slug = slug
            while slug_exists:            
                slug_exits = Topic.objects.filter(slug=slug).exists()
                if slug_exits:
                    slug = self.slug + '_' + str(counter)
                    counter += 1
                else:
                    self.slug = slug
                    break
        super(Topic, self).save(*args, **kwargs)

And when I add a new post into the topic I see in the terminal:

going to update lastposted 2018-06-25 03:45:11+00:00
new topic lastposted time 2018-06-25 15:12:08.469940+00:00

But when I see the field in phpMyAdmin the database, it still has the same old value, i.e 2018-06-25 03:45:11.

The odd thing is that sometimes the update takes place.

What could be wrong here? How can I fix this?

1

There are 1 answers

0
Rahmi Pruitt On

Try

super(Topic, self).save(args, kwargs)

Using * twice should cause an error.