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-commands
 but 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.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to