get_or_create() is probably perfect, I just haven't come across it in
the djangobook yet. I'm a newb. Gonna have to start combing the docs a
little more thoroughly! Thanks for the suggestion!

On Jun 13, 4:32 pm, Christian Schilling <initcr...@googlemail.com>
wrote:
> the most obvious reason why this is bad is: it will stop working when
> you switch
> to an other database backend.
> but aside from that: what are you trying to archive?
> looks a lot like what the get_or_create() shortcut does...
>
> On Jun 13, 8:42 am, koepked <koep...@hotmail.com> wrote:
>
> > Is it bad practice to rely on db exceptions to indicate an attempt at
> > writing duplicate values to a "keyed" column? For example, in some
> > code I'm working on, I have the following:
>
> > x = ContentItem(title=e_title)
>
> > try:
> >      x.save()
> > except MySQLError:
> >      x = ContentItem.objects.get(title=e_title)
>
> > for tag_title in tag_titles:
> >      y = Tag(title=tag_title)
>
> >      try:
> >           y.save()
> >      except MySQLError:
> >           y = Tag.objects.get(title=e_title)
>
> >      x.tags.add(y)
>
> > Catching the MySQLError indicates to me that that title already exists
> > in the db, so I then retrieve it instead of inserting it. I have a
> > feeling that some folks will say this is bad, but if so, I'm curious
> > why. To me, it seems better than significantly increasing the # of
> > queries I have to run to check if the data's already there before I
> > write it. Thanks for any help.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to