Ingxaki enkulu kukuba umhla wohlaziyo lwe-auto awuphathwanga ngokufanelekileyo nguDjango. Ukuba uzama ukuhlaziya intsimi yomhla usebenzisa i-interface yomlawuli, i-Django iya kuhlaziya ngokuzenzekelayo zonke iindawo etafileni ngegama elifanayo, kubandakanywa nomhlaba wohlaziyo oluzenzekelayo. Oku kunokukhokelela kutshintsho olungalindelekanga nolunokubakho olungachanekanga kwidatha yakho.
I have a model with a date field. I want to automatically update the date field when an object is created or edited in the admin panel. How can I do this? A: You can override <code>save_model</code> method of your admin class and set the value of <code>date</code> field to <code>datetime.now()</code>. <blockquote> <p><strong><a href="https://docs.djangoproject.com/en/2.1/ref/contrib/admin/#django.contrib.admin.ModelAdmin" rel="nofollow noreferrer">ModelAdmin</a></strong></p> <p><strong><a href="https://docs.djangoproject.com/en/2.1/ref/contrib/admin/#django-contrib-admin-options-modeladmin-save_model" rel="nofollow noreferrer">save_model()</a></strong></p> <p>[...] This method should save the object and return the new instance.</p> </blockquote> Example: <code>class MyModelAdmin(admin, ModelAdmin): def save_model(self, request, obj, form, change): if not change: # obj is being created for first time (i e., it's new) obj = super().save_model(request, obj, form, change) # create object first # now set your custom values here... obj = super().save_model(request, obj, form=form._meta['fields'], change=change) # update object with custom values else: # existing object is being edited (i e., it's not new) # now set your custom values here... obj = super().save_model(request, obj=obj._meta['fields'], form=form._meta['fields'], change=change) # update object with custom values return obj </code>
Yintoni indawo yohlaziyo oluzenzekelayo
Indawo ethi "Uhlaziyo oluzenzekelayo" yintsimi ye-boolean kwitheyibhile "yeseto" yeprojekthi ye-Django. Xa isetelwe kwiNyaniso, i-Django iya kukhangela ngokuzenzekelayo uhlaziyo kwiifayile zoseto lweprojekthi kwaye, ukuba kukho naziphi na iisetingi ezintsha ezifunyenweyo, ziya kuhlaziya iifayile zeprojekthi.