How to know if is a update or insert??
Hi, I overrided the save_model of ModelAdmin of model Client that is associated a one User so I can create a user when a client is created. Well, how to know if the save_model was called to do a update or a insert if the object already exists in database? Trying to see if model.pk exists fail. Thanks in advance. Andre -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: How to know if is a update or insert??
try: old_user = User.objects.get(pk=self.pk) # means update except: pass 2011/2/4 andmart > Hi, > > I overrided the save_model of ModelAdmin of model Client that is > associated a one User so I can create a user when a client is created. > > Well, how to know if the save_model was called to do a update or a > insert if the object already exists in database? > > Trying to see if model.pk exists fail. > > Thanks in advance. > > Andre > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-users@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com > . > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: How to know if is a update or insert??
On Fri, Feb 4, 2011 at 10:13 PM, andmart wrote: > Hi, > > I overrided the save_model of ModelAdmin of model Client that is > associated a one User so I can create a user when a client is created. > > Well, how to know if the save_model was called to do a update or a > insert if the object already exists in database? > > Trying to see if model.pk exists fail. Hi, I've never had problemas with: def save(self, *args): if self.pk: #update else: #insert this is how you check if model.pk exists? -- Marc -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: How to know if is a update or insert??
In a insert or in a update, the pk is already filled in ModelAdmin.save_model. The creation of object in database is in ModelForm.save. So, I think it's not the way to handle it. I notivced googling there is a boolean parameter named change and I'm using it. On 4 fev, 21:09, Marc Aymerich wrote: > On Fri, Feb 4, 2011 at 10:13 PM, andmart wrote: > > Hi, > > > I overrided the save_model of ModelAdmin of model Client that is > > associated a one User so I can create a user when a client is created. > > > Well, how to know if the save_model was called to do a update or a > > insert if the object already exists in database? > > > Trying to see if model.pk exists fail. > > Hi, I've never had problemas with: > > def save(self, *args): > if self.pk: > #update > else: > #insert > > this is how you check if model.pk exists? > > -- > Marc -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: How to know if is a update or insert??
On Sun, Feb 6, 2011 at 2:53 PM, andmart wrote: > In a insert or in a update, the pk is already filled in > ModelAdmin.save_model. > > The creation of object in database is in ModelForm.save. > > So, I think it's not the way to handle it. > > I notivced googling there is a boolean parameter named change and I'm > using it. Hi andmart, Do you have more information about this boolean parameter? do you have the link to the website where you find it? what you comment seems quite useful :) -- Marc -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: How to know if is a update or insert??
Marc, I found a reference in docs. http://docs.djangoproject.com/en/1.2/ref/contrib/admin/#modeladmin-methods On Feb 6, 2:00 pm, Marc Aymerich wrote: > On Sun, Feb 6, 2011 at 2:53 PM, andmart wrote: > > In a insert or in a update, the pk is already filled in > > ModelAdmin.save_model. > > > The creation of object in database is in ModelForm.save. > > > So, I think it's not the way to handle it. > > > I notivced googling there is a boolean parameter named change and I'm > > using it. > > Hi andmart, > Do you have more information about this boolean parameter? do you have > the link to the website where you find it? > what you comment seems quite useful :) > > -- > Marc -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.