Quantcast
Channel: In a django model custom save() method, how should you identify a new object? - Stack Overflow
Browsing latest articles
Browse All 16 View Live

Answer by Gaurav Jain for In a django model custom save() method, how should...

A more modern approach For Python3.8 and above using the walrus operator. (if you want to save some lines of code)def save(self, *args, **kwargs): if is_new_obj := self._state.adding: # Do some...

View Article



Answer by user3486626 for In a django model custom save() method, how should...

In python 3 and django 3 this is what's working in my project:def save_model(self, request, obj, form, change): if not change: #put your code here when adding a new object.

View Article

Answer by Swelan Auguste for In a django model custom save() method, how...

> def save_model(self, request, obj, form, change):> if form.instance._state.adding:> form.instance.author = request.user> super().save_model(request, obj, form, change)> else:>...

View Article

Answer by Sachin for In a django model custom save() method, how should you...

Would this work for all the above scenarios?if self.pk is not None and <ModelName>.objects.filter(pk=self.pk).exists():...

View Article

Answer by metakermit for In a django model custom save() method, how should...

For a solution that also works even when you have a UUIDField as a primary key (which as others have noted isn't None if you just override save), you can plug into Django's post_save signal. Add this...

View Article


Answer by SaintTail for In a django model custom save() method, how should...

Alternative way to checking self.pk we can check self._state of the model self._state.adding is True creatingself._state.adding is False updatingI got it from this page

View Article

Answer by Jordan for In a django model custom save() method, how should you...

I'm very late to this conversation, but I ran into a problem with the self.pk being populated when it has a default value associated with it.The way I got around this is adding a date_created field to...

View Article

Answer by Kwaw Annor for In a django model custom save() method, how should...

Check for self.id and the force_insert flag.if not self.pk or kwargs.get('force_insert', False): self.created = True# call save method.super(self.__class__, self).save(*args, **kwargs)#Do all your post...

View Article


Answer by JF Simon for In a django model custom save() method, how should you...

You could just connect to post_save signal which sends a "created" kwargs, if true, your object has been inserted.http://docs.djangoproject.com/en/stable/ref/signals/#post-save

View Article


Answer by ha22109 for In a django model custom save() method, how should you...

To know whether you are updating or inserting the object (data), use self.instance.fieldname in your form. Define a clean function in your form and check whether the current value entry is same as the...

View Article

Answer by yedpodtrzitko for In a django model custom save() method, how...

rather use pk instead of id:if not self.pk: do_something()

View Article

Answer by KayEss for In a django model custom save() method, how should you...

The check for self.pk == None is not sufficient to determine if the object is going to be inserted or updated in the database.The Django O/RM features an especially nasty hack which is basically to...

View Article

Answer by Gerry for In a django model custom save() method, how should you...

Checking self.id assumes that id is the primary key for the model. A more generic way would be to use the pk shortcut.is_new = self.pk is None

View Article


Answer by vikingosegundo for In a django model custom save() method, how...

It is the common way to do so.the id will be given while saved first time to the db

View Article

Answer by Dave W. Smith for In a django model custom save() method, how...

Updated: With the clarification that self._state is not a private instance variable, but named that way to avoid conflicts, checking self._state.adding is now the preferable way to check.self.pk is...

View Article


In a django model custom save() method, how should you identify a new object?

I want to trigger a special action in the save() method of a Django Model object when I'm saving a new record (not updating an existing record.)Is the check for (self.id != None) necessary and...

View Article
Browsing latest articles
Browse All 16 View Live




Latest Images