3

Following is my model:

class MyModel(models.Model):
    code = models.CharField(_('Code'), max_length=56, null=False,
                        blank=False, db_column='code')
    name = models.CharField(_('Name'), max_length=128, null=False,
                        db_index=True, db_column='name', blank=False)
    created_at = models.DateTimeField(_('Created At'), null=False,
                                      db_column='created_at', blank=False,
                                      auto_now_add=True)

Form:

class MyForm(forms.Form):
    class Meta:
        model = MyModel 
        fields = '__all__'

view

class MyView(View):

    def get(self, request):
        form = MyForm(request=request)
        return render(request, 'test_manager/testrule_form.html', {'form': form})

When I display this form, I am able to see all fields excluding created_at. Does anyone know how do I get created_at field while updating certain object?

Diksha
  • 384
  • 1
  • 5
  • 16

1 Answers1

6

If you specify auto_now=True, or auto_now_add=True, then you make the field editable=False at the same time. Hence it means it will not show up in the form.

Probably a minimal change would be to specify a default value by using a function, instead of using the auto_now_add=True property, like:

from django.utils import timezone as tz

class TestRule(models.Model):
    code = models.CharField(_('Code'), max_length=56, null=False,
                        blank=False, db_column='code')
    name = models.CharField(_('Name'), max_length=128, null=False,
                        db_index=True, db_column='name', blank=False)
    created_at = models.DateTimeField(_('Created At'), null=False,
                                      db_column='created_at', blank=False,
                                      default=tz.now)

Note that we do not call the now(..) function. If we would do that, then we would set the created_at column to the timestamp when we constructed the models (not the instances of the model). By passing a reference to the now(..) function, the function will be called when a new model instance is added.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • Hint: the [docs](https://docs.djangoproject.com/en/2.0/topics/i18n/timezones/#naive-and-aware-datetime-objects) suggest using `from django.utils import timezone as tz` and then `tz.now` instead of `datetime.now`. I don't know why they didn't change all of their examples yet in the official docs. – Ralf May 30 '18 at 11:49
  • @DikshaJiwane: well that is in the answer: setting a `default=tz.now`. `auto_now` actually has more implications: it makes the field not editable. – Willem Van Onsem May 30 '18 at 12:52
  • @DikshaJiwane: ah, you mean `auto_now` (instead of `auto_now_add`)? – Willem Van Onsem May 30 '18 at 12:54
  • @DikshaJiwane: for `auto_now_add` we can use this `default=...` approach. But for `default_now` it would be very strange to add that to a form, since `auto_now` means that it should take the time when it is saved. – Willem Van Onsem May 30 '18 at 13:04