views:

76

answers:

2

I have been developing some Django app and there's some duplicated code for different Models. I'd like to create a generic table template and pass the Model class, a list of model instances, and Form classes to it so it can render the page and generate the forms to add/delete elements. Then create some generic add/delete views to work with this Forms.

Which would be the correct part to define the configuration of the template for every different Model? Would it be right if I just create some class static variables and functions like:

class Test(models.Model):
    # Model
    name = models.CharField(max_length=20)
    description = models.TextField(blank=True)

    # Template configuration 
    title = "Test"
    table_columns = ['name', ] # Columns I want to show in the table

    def get_columns(self):
        return [self.name, ]

Or is there some cleaner way to define this kind of things in Django?

EDIT: Seems like some of the information I want to use to configure the Template already has a name and should go inside model.Meta, like verbose_name or verbose_name_plural.

A: 

I'm not exaclty sure about your question and your code, but here is a short story about _meta ...

To access the column name of a class, you can inspect the _meta attribute of the class.

Example. A sample model, which defines three fields and a helper methods whats_inside, which just iterates over _meta.fields and prints out the names of the columns of the class:

from django.db import models

def whats_inside(cls):
    for item in cls._meta.fields:
        print item.name

class Sample(models.Model):

    name = models.CharField(max_length=80)
    desc = models.CharField(max_length=80)
    date_added = models.DateTimeField(auto_now=True, auto_now_add=True)

When we start ./manage shell, we can call whats_inside with Sample as argument (note: ms is just the package the model is located in this case, yours will differ):

$ ./manage.py shell 
Python 2.6 (r26:66714, Oct  4 2008, 02:48:43) 
Type "copyright", "credits" or "license" for more information.

IPython 0.9.1 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object'. ?object also works, ?? prints more.

In [1]: from ms.models import Sample, whats_inside

In [2]: whats_inside(Sample)
id
name
desc
date_added

In [3]:
The MYYN
That's useful thanks, didn't know about _meta, but in this case I want to configure which columns are going to be showed for each Model, not all of them because in some cases there's too many columns or some of them aren't very informative.My question is more about if there's a standard place to put this kind of configuration of a generic template for any Model it contains, and if there's no standard place then if the Model definition is ok.
plmet
A: 

As MYYN says, your question is not very clear. However i assume you would like to do the following:

  1. Create a generic template
  2. Dynamically load this template based on the model and other params passed
  3. The table in the template is populated with the necessary forms etc
  4. Create views, for the forms to submit to

I would use a custom template tag rather then including the code in your model (since its purely a presentation issue, i.e you would like all the fields available in the model but only display some of them). For example you could create a template tag which you call like:

{% gen_table somemodel %}

Documentation for template tags

Also read this great post on custom template tags and some of my sample code.

I also noticed you seem to be defining a custom method in your model class, however i believe the way to do this is by creating a custom model manager (read this blog post), also an example can be found here

izzy