Hey, On Tue, Feb 17, 2009 at 2:33 PM, Anders <anders.grimsrud.erik...@gmail.com> wrote: > > Hi. > > I do a lot of csv-import of data and have previously been using Ruby > on Rails. And the only thing I miss about Rails (apart from database > migration) is the find_or_create_by methods. > > Are there any similar methods in Django? >
Yes, take a look at get_or_create: http://www.djangoproject.com/documentation/models/get_or_create/ > My use is like this: > > I import a CSV file, and in one column there is a value (i.e. a > string). I don't want to store it as a string in Model1, but as a > foreign key to Model2 - thus trying to find a record that has the > string and returning the foreign key id if found - else create the > object in Model2. > > From the rails api: > > # No 'Summer' tag exists > Tag.find_or_create_by_name("Summer") # equal to Tag.create(:name => > "Summer") > > # Now the 'Summer' tag does exist > Tag.find_or_create_by_name("Summer") # equal to Tag.find_by_name > ("Summer") > > # Now 'Bob' exist and is an 'admin' > User.find_or_create_by_name('Bob', :age => 40) { |u| u.admin = > true } > > I know it's probably bad to say that Rails is good in some ways in a > Django forum, so forgive me :) > Why? Matthias --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---