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

iphone - Dismissing a UIAlertView -

c# - Can ProtoBuf-Net deserialize to a flat class? -

javascript - Change element in each JQuery tab to dynamically generated colors -