I am using a custom model manager in addition to the default manager (objects=model.Manager()). I would like to add two __unicode__ methods to the model — one for the default manager and one for the custom manager; otherwise, the unicode throws an error for the result of the custom manager, which does not contain some of the variables used in the unicode for the default manager.
For example —
class CreditCountManager(models.Manager):
def credit_count(self):
...
return result_list
class UserProfile(models.Model):
user = models.ForeignKey(User, unique=True)
full_name = models.CharField(max_length=100)
network = models.ForeignKey(Network)
...
credit_count = CreditCountManager()
objects = models.Manager()
# need something like:
def __unicode-for-default__(self):
return "%s(%s)"%(self.user, self.network)
def __unicode-for-credit_count__(self):
return "%s(%s)"%(other variables)
Is there a way to do this? Thank you.
Your question does not state what kind of error you have and thus is incomplete.
Unfortunately unicode is a Python construct. You can have just one off them.
Instead, you need to fix the place were the object is formatted to a string to use a custom method.
E.g.
instead of
If the problem is an exception risen inside unicode() method you need to write the method in such a way that the exception does not occur. E.g. don’t use uninitialized variables.