Posts

Showing posts from November, 2020

on_delete in Django models

Below are the possible actions with on_delete in Django Models: CASCADE: When the referenced object is deleted, also delete the objects that have references to it. Ex: When you remove a Sales Order Header for instance, it will delete Sales Order Lines as well).  PROTECT:  To delete it you will have to delete all objects that reference it manually. RESTRICT:  Similar behavior as PROTECT that matches more accurately.  SET_NULL: Set the reference to NULL. For instance, when you delete a User, you might want to keep the comments he posted on blog posts, but say it was posted by an anonymous or deleted user.  SET_DEFAULT: Set the default value. SET: Set a given value. DO_NOTHING: This will create integrity issues in your database.

How to make a field readonly in Django form

 self.fields['XX_FIELD_NAME'].widget.attrs['readonly'] = True

How to change a Django form field to a hidden field

 self.fields['XX_FIELD_NAME'].widget = forms.HiddenInput()