I'm not sure if it is possible, but I'm trying to add a model manager
defined outside of the model.py to a modelclass, in a similar way as I
add functions to a model class. (my model classes (> 100) are
automatically generated from the database and I don't want to edit
that file every time)

this is how I add a new function to a model class

## in models.py
class Customer(models.Model):
    customer_id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=255)
    companyname = models.CharField(max_length=255)
    active = models.BooleanField()

## in modeldefs.py
def unicode_name(self):
    return ("%s [%s]" %(self.name, self.companyname)

## in __init__.py
from cust.modeldefs import unicode_name
setattr(Customer, '__unicode__', unicode_name)

So far so good, the function unicode_name is added to the customer
class as __unicode__ function. This way I am writing the function only
once and I can add it to as many models as I want (as far as the model
fields match of course)

Now I want to add a custom manager..

I can define the manager as follows (directly adapted from the
manual) inside models.py :

class CustomerManager(models.Manager):
    def get_query_set(self):
        return super(CustomerManager,
self).get_query_set().filter(active=True)

class Customer(models.Model):
    customer_id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=255)
    companyname = models.CharField(max_length=255)
    active = models.BooleanField()

    activecustomers = CustomerManager()

Querying Customer.activecustomers.all() gives me all active customers.
However, what I WOULD like to do, is not to define the manager in
models.py and add the manager in the Customer class definition, but I
would like to use a similar method as with the class functions:

Thus:
Define the CustomerManager in modeldefs.py and then;

## in __init__.py
from cust.modeldefs import unicode_name, CustomerManager
setattr(Customer, '__unicode__', unicode_name)
setattr(Customer, 'activecustomers', CustomerManager)

However, when I do this I get a 'NoneType' object has no attribute
'_meta'

Is it possible at all to add a manager to a model class afterwards?

Vidja


-- 
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