I've got a model with field datetime = models.DateTimeField(auto_now_add=True)
. In form created from this model I don't need to edit this field but I need to show it. The fields with auto_now_add=True
automatically isn't shown. What is the best way to show this field?
Instead, django-read-only uses always installed database instrumentation to inspect executed queries and only allow those which look like reads.
The is_valid() method is used to perform validation for each field of the form, it is defined in Django Form class. It returns True if data is valid and place all data into a cleaned_data attribute.
Bound and unbound forms If it's bound to a set of data, it's capable of validating that data and rendering the form as HTML with the data displayed in the HTML. If it's unbound, it cannot do validation (because there's no data to validate!), but it can still render the blank form as HTML.
The simplest way is by using the field option blank=True (docs.djangoproject.com/en/dev/ref/models/fields/#blank).
Assuming you pass an instance
when creating form object, you can access it in your template using:
{{ form.instance.datetime|date:"Y/m/d H:i:s" }}
If you don't have an instance, though, the smartest thing to do would be to use current time:
{% now "Y/m/d H:i:s" %}
You can display the instance value when editing form like this:
{{ form.instance.datetime }}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With