Cats and Code » python http://blog.gorwits.me.uk by Oliver Gorwits Sat, 29 Mar 2014 23:28:44 +0000 en-US hourly 1 http://wordpress.org/?v=3.6.1 Django Admin App Views http://blog.gorwits.me.uk/2011/03/03/django-admin-app-views/?utm_source=rss&utm_medium=rss&utm_campaign=django-admin-app-views http://blog.gorwits.me.uk/2011/03/03/django-admin-app-views/#comments Thu, 03 Mar 2011 09:29:13 +0000 Oliver Gorwits http://blog.gorwits.me.uk/?p=422 Continue reading ]]> Following my previous post about configuring Django models, I’m now ready to work on the view. Remember, this is simply within the “Admin” app which ships with Django, and below is a non-exhaustive set of options:

ModelAdmin Derived Class Options

For the list screen:

  • list_display = ('field1', 'field2')
  • list_display_links = ['field2']
  • list_editable = ['field2']
  • list_filter = ('field1', 'field2')
  • list_per_page = 20
  • search_fields = ['field1', 'foreign_key__fieldname']

For the edit screen, it’s possible to control the form layout and displayed fields:

fieldsets = (("Title"|None, options), )
options = {
    fields : (('one', 'line', 'together'), 'field5', 'field6'),
    classes : ['collapse', 'wide']
}

or use the following:

fields = ('field1', 'field2')
exclude = ('field3', 'field4')

And some other edit screen options:

  • filter_horizontal|filter_vertical = ('multi_select_field')
  • radio_fields = ('fk_field', 'choice_field')
  • save_on_top = True

For further details, see the Admin App documentation. Also useful are the Custom Actions documentation, the DB Queries documentation, and the QuerySet documentation.

]]>
http://blog.gorwits.me.uk/2011/03/03/django-admin-app-views/feed/ 0
Django Models http://blog.gorwits.me.uk/2011/03/02/django-models/?utm_source=rss&utm_medium=rss&utm_campaign=django-models http://blog.gorwits.me.uk/2011/03/02/django-models/#comments Wed, 02 Mar 2011 14:38:53 +0000 Oliver Gorwits http://blog.gorwits.me.uk/?p=416 Continue reading ]]> I’ve been working on a small Django app recently, and started by cleaning up its model definitions. Here’s a quick (but not exhaustive) list of useful configuration options:

Model Meta Options

  • db_table = "string"
  • managed = False (whether SQL is generated by the manage.py app)
  • ordering = ['column1', '-column2'] (NB admin app only uses the first)
  • order_with_respect_to = 'fk_field'
  • unique_together = (('column1', 'column2'), )
  • verbose_name = "String"
  • verbose_name_plural = "String"
  • abstract = True

Common Field Options

  • null = True
  • blank = True
  • choices = (('db_val', 'displayed_val'), )
  • db_column = "string"
  • db_index = True
  • default = "string"
  • editable = False
  • primary_key = True
  • unique = True
  • verbose_name = "String"

Field Types and Options

  • AutoField
  • BooleanField
  • CharField (max_length=integer)
  • DateField|DateTimeField(auto_now_add=True)
  • IPAddressField
  • PositiveIntegerField
  • TextField
  • ForeignKey(Class, to_field='related_field')
  • ManyToManyField(Class, to_field='related_field')

For further details, see the Models documentation, Meta Options documentation and Field Types documentation.

]]>
http://blog.gorwits.me.uk/2011/03/02/django-models/feed/ 0