Login

Snippets by grahamu

Snippet List

more on manager methods

[Snippet #2](http://www.djangosnippets.org/snippets/2/) demonstrated some cool tricks possible with manager methods. This example shows how to assign and use a custom manager method. In this snippet the `belongs_to_user` method returns an Account queryset containing only those accounts associated with the specified user. The method is useful because it hides the implementation of User in the Account model. Line 17 associates the custom manager with the Account model.

  • managers
Read More

Modify fields created by form_for_model

Using newforms you can create forms from existing models easily and automatically with either `form_for_model(class)` or `form_for_instance(instance)`. Usually the automagically generated form fields are sufficient; however, sometimes you need to restrict selections in a choice field. You can also set the default selection when instantiating the form. In this example, if `acct` is not contained in the 'account' field choices, the selection defaults to the first entry. This example is probably not good practice when using `form_for_instance` because the existing value 'selection' of the choice field is lost and must be reset manually (see above).

  • newforms
  • foreignkey
  • dropdown
  • choicefield
  • form_for_model
  • form_for_instance
Read More

grahamu has posted 2 snippets.