Alternative way to checking self.pk
we can check self._state
of the model
self._state.adding is True
creating
self._state.adding is False
updating
I got it from this page
More Related Contents:
- Extending the User model with custom fields in Django
- What’s the difference between django OneToOneField and ForeignKey?
- How to set a Django model field’s default value to a function call / callable (e.g., a date relative to the time of model object creation)
- How to define two fields “unique” as couple
- Django: Get model from string?
- Django: Get list of model fields?
- How to filter empty or NULL names in a QuerySet?
- How do I get Django Admin to delete files when I remove an object from the database/model?
- How to view corresponding SQL query of the Django ORM’s queryset?
- Unique fields that allow nulls in Django
- Django ModelForm: What is save(commit=False) used for?
- Tying in to Django Admin’s Model History
- How do I add a Foreign Key Field to a ModelForm in Django?
- Django: Validate file type of uploaded file
- Django data migration when changing a field to ManyToMany
- django dynamically filtering with q objects
- How to limit queryset/the records to view in Django admin site?
- How to assign currently logged in user as default value for a model field?
- Django: implementing JOIN using Django ORM?
- Django – Simple custom template tag example
- Django user profile
- How can I restrict Django’s GenericForeignKey to a list of models?
- Django ManyToMany model validation
- how to show datepicker calendar on datefield
- Django – Overriding the Model.create() method?
- Multiple images per Model
- Can Django automatically create a related one-to-one model?
- Limit number of model instances to be created – django
- Django query datetime for objects older than 5 hours
- How to add Check Constraints for Django Model fields?