save or update for a model in Django Admin -



save or update for a model in Django Admin -

i have next structure:

class a(models.model): = models.foreignkey(b, unique=true) b = models.integerfield(default=0, blank=true) def save(self, *args, **kwargs): self.b += 1 super(a, self).save(*args, **kwargs)

i wanted increment "b" 1 whenever saved. works fine when add together item first time otherwise fails because of "uniquetrue" clause.

how allow django "update if exists" otherwise "create new". model increments count.

thanks.

the creation happens when object. if have problem saving duplicates, should looking.

a.objects.get_or_create()

should object if exists, create otherwise. can examine it's pk see if is same object, or duplicate. refer the documentation.

django django-models django-admin

Comments

Popular posts from this blog

c# - Move local mssql database to webhosted MYSQL -

java - How to pass parameters between Request-Scoped Controllers? -

string - what is the difference between "some" == "some\0" and strcmp("some","some\0") in c++? -