After a bunch of trial and error, basically, I've decided not to put the url pattern in the ProvinceAdmin class. It is easier and makes more sense (to me) to add the urls to myproject/urls.py and then create a file called myproject/myapp/admin_views.py with the custom admin views. It's probably possible to do it the other way, but I ran into problems with trying to re-register the models (since they are being registered in admin.py and admin_views.py) and the urls are easier to deal with in admin_views.py. Note that this is only necessary for extending admin views/functionality. If you're happy with the admin views that ship with Django, don't worry about any of this. I was just having trouble adding a new template and view to my admin pages and it is considerably different (in my opinion) from writing a standard view (as covered in the django tutorials).
To summarize what I did to make a new button on the admin site to populate all provinces with random samurai: *added (r'^admin/myapp/province/populate_japan/$', 'myapp.admin_views.populate_japan') to myproject/urls.py *added a button to the myproject/templates\admin\v1\province\change_list.html template (copied from django.contrib.templates.admin.templates) called Populate Japan. The button points to the url pattern above. *created a file myproject/myapp/admin_views.py which imports models and forms, and contains the functions and logic to perform the task *created a form (inheriting from the forms.Form class) which allows me to choose how many samurai to add at one time. This is located in the admin_views.py file *wrote a view called populate_japan in the admin_views.py file (where the above url points to). The view instantiates and handles the form. After a successful POST, it extracts the number of samurai to add, and calls a function which performs the actual random generation and updates the databases via the MODEL.save() method. The function is inside admin_views.py but could be in a support_functions.py file to be imported when necessary (since I don't really need to populate japan everyday) *after the random generation function returns the populate_japan view renders an html response detailing the names and locations of the created samurai, and printing how much time the creation took. I could have used a template to do this but honestly found it easier to just print them. If I had known it was this much trouble to add a button to the admin page, I might have just written a json file. But hopefully I learned something in the process and can do it more easily in the future. The only real concern I have with this implementation is that it is quite slow to create random samurai. It takes almost exactly 1 second per samurai, which is crazy. I think the problem is that I am inefficiently calling the database and saving records one at a time. I need to see how the most efficiently write a bulk insert method and then just skip the model ORM and insert them. This is another argument for just writing the json file, actually. If anyone has comments on how best to create a large number of objects at runtime (say 100 to 500), I'd be happy to hear them. Hopefully, this helps someone else. -Tim On Mon, Feb 22, 2010 at 12:18 PM, Timothy Kinney <timothyjkin...@gmail.com>wrote: > Okay, I got it. > > There was a typo in the hard-coded url pattern. The following actually does > not work: > ** code ** > > (r'^/1/add_random_samurai/$', self.admin_site.admin_view( > self.add_random_samurai)) > ** /code ** > > But this does: > ** code ** > > (r'^1/add_random_samurai/$', > self.admin_site.admin_view(self.add_random_samurai)) > ** /code ** > > Basically, I added an extra slash and that's why it wasn't picking it up. > So to catch any number, I just use: > ** code ** > (r'^\d+/add_random_samurai/$', > self.admin_site.admin_view(self.add_random_samurai)) > ** / code ** > > I'm still not sure if this will create the samurai in the correct province. > I think I can extract this information from the request though. We shall > see. > > -Tim > > > > On Mon, Feb 22, 2010 at 12:08 AM, Timothy Kinney <timothyjkin...@gmail.com > > wrote: > >> Okay, I've got such a button and I have copied a template from admin to >> serve as a stand-in for it. I have created functions under ProvinceAdmin (in >> myapp\admin.py) that look like: >> >> ** code ** >> def ProvinceAdmin(admin.ModelAdmin): >> # other stuff >> >> def add_random_samurai(self, request): >> from django.http import HttpResponse >> # do stuff >> return HttpResponse("This is where the template will render.") >> >> def get_urls(self): >> from django.conf.urls.defaults import patterns >> urls = super(ProvinceAdmin, self).get_urls() >> my_urls = patterns('', >> (r'^/(?P<province_id>\d+)/add_random_samurai/$', >> self.admin_site.admin_view(self.add_random_samurai)) >> ) >> return my_urls + urls >> ** / code ** >> >> But I'm getting a ValueError: >> invalid literal for int() with base 10: '1/add_random_samurai' >> >> If I hardcode: (r'^/1/add_random_samurai/$', >> self.admin_site.admin_view(self.add_random_samurai)) >> Then it works. >> >> So I think my regular expression is not picking up the right integer. Can >> you help me re-write the regular expression to catch the province number? >> >> Or can I ignore the province number since I am presumably calling a >> function from within the instance of the province I want to update? If so, >> how do I tell the pattern matching to ignore that? >> >> -Tim >> >> >> On Fri, Feb 19, 2010 at 5:21 PM, Peter Herndon <tphern...@gmail.com>wrote: >> >>> >>> On Feb 19, 2010, at 5:51 PM, Timothy Kinney wrote: >>> >>> > So I have a nice little database now stocked with items and provinces. >>> > I now want to generate random samurai and populate the database with >>> > them. I can think of two ways to do this: >>> > >>> > 1) Through the admin interface. But how do I install a button that >>> > will add a random samurai? Adding samurai is a built-in function on >>> > the template, but the fields are always empty. Is there a >>> > straightforward way to add another button called "Add Random Samurai" >>> > that does the same thing but with the fields randomly filled from >>> > appropriate choices? >>> > >>> > 2) Use a python script. This seems to have two possible methods: >>> > a) Randomly generate samurai desired, output a JSON flatpage, and call >>> > manage.py loaddata that_flatpage >>> > >>> > b) Randomly generate the samurai desired, access the database directly >>> > and insert them using SQL syntax. (not very Django like) >>> > >>> > I have listed these in order of preference. Can someone tell me the >>> > easiest way to implement a new admin button? I'm not even sure where >>> > the admin templates are stored. :/ >>> > >>> > -Tim >>> >>> Hi Tim, >>> >>> Docs for overriding admin are here: >>> http://docs.djangoproject.com/en/1.1/ref/contrib/admin/#overriding-admin-templates >>> >>> You will probably want to override the change_form.html template at >>> whatever level is appropriate for your needs (Province, if you are adding >>> Samurai to random rooms), and add a button "Generate Random Samurai". That >>> button will be the submit for a form that points to a view you will write. >>> That view should generate a random number, loop over that number, create a >>> Samurai object and assign it to a randomly-chosen Room (pick a random number >>> from 1 through the total number of rooms, get the room via "room = >>> Room.objects.get(pk=<random number>"). You will need to add a URL that will >>> tie together the view and the submit button. >>> >>> For an added bonus, add an IntegerField to your form allowing you to set >>> an upper bound on the number of Samurai generated. >>> >>> An approach similar to 2b would be to implement a custom management >>> command (skeletal docs here: >>> http://docs.djangoproject.com/en/1.1/howto/custom-management-commands/#howto-custom-management-commandsbut >>> Google for better examples) that would allow you to run "python >>> manage.py create_samurai". That command would use the same logic as I >>> outlined for the view, and create Samurai via the ORM and assign them to >>> random Rooms. >>> >>> ---Peter Herndon >>> >>> -- >>> You received this message because you are subscribed to the Google Groups >>> "Django users" group. >>> To post to this group, send email to django-us...@googlegroups.com. >>> To unsubscribe from this group, send email to >>> django-users+unsubscr...@googlegroups.com<django-users%2bunsubscr...@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-us...@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.