0

I am trying to create a method(s) on my model to return the 'long name' for my choices. I obviously do not understand the "Model.get_FOO_display()" method, or am doing some syntactic butchery. The ultimate plan is to grab the 'long names' in the (haystack) search_index.py using (model_attr='xxx'). I've done a few hours of searching and have not found an example that works. I appreciate any suggestions you can offer. Thanks.

Models.py

new_used = (
    ('n', 'new'),
    ('u', 'used'),
)

broker = (
    ('n', 'No'),
    ('y', 'Yes'),
)

class Flist(models.Model):
    createdate = models.DateTimeField(auto_now_add=True)
    expirydate = models.DateTimeField(null=True, blank=True)
    price = models.IntegerField(null=True, blank=True)
    new_used = models.CharField(max_length=1, blank=True, choices=new_used, default='u')    
    broker_y_n = models.CharField(max_length=1, blank=True, choices=broker, default='n')
    mfr = models.CharField(max_length=80, blank=True)    
    listing_type = models.ForeignKey(ListingType)
    listing_status = models.ForeignKey(ListingStatus, default=3)
    location_zip = models.IntegerField(null=True, blank=True)    
    customer = models.ForeignKey(Customer)

    def nu(self):
        return Flist.get_new_used_display

    def byn(self):
        return Flist.get_broker_y_n_display 

search_indexes.py

class FlistIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)    
    createdate = indexes.DateTimeField(model_attr='createdate', faceted=True)
    price = indexes.IntegerField(model_attr='price',  faceted=True)
    new_used = indexes.CharField(model_attr='nu',  faceted=True)
    broker_y_n = indexes.CharField(model_attr='byn',  faceted=True)
    mfr = indexes.CharField(model_attr='mfr',  faceted=True)
    listing_type = indexes.CharField(model_attr='listing_type',  faceted=True)
    listing_status = indexes.CharField(model_attr='listing_status',  faceted=True)
    location_zip = indexes.IntegerField(model_attr='location_zip',  faceted=True)

    def get_model(self):
        return Flist

THE QUESTION CONTINUES.....

Ok changed "bad' methods to properties:

    @property
    def nu(self):
       return self.get_new_used_display()

    @property
    def byn(self):
       return self.broker_y_n_display()

Appears haystack does not like (can't find_ model_attr(ibutes) 'byn' or 'nu' so won't index.

Before I started with methods and @property I was getting names and facet counts but the names were the short versions. What is the proper syntax to get the long name to the model_attr in FlistIndex?

BillB1951
  • 225
  • 6
  • 17

1 Answers1

2

You're returning the method itself, not the value returned by the method, i.e. instead of:

def nu(self):
    return self.get_new_used_display

You need to do:

def nu(self):
    return self.get_new_used_display()

Above code edited to use self instead of Flist (instance versus class) as @DanielRoseman pointed out.

UPDATE

Also, FWIW, if your going to wrap the method calls into shorter versions (e.g. .nu() is equivalent to .get_new_used_display(), then you might as well make it truly worthwhile and turn it into a property:

@property
def nu(self):
    return self.get_new_used_display()

Then, you can just do some_flist.nu (notice without parenthesis) like any other property on your model. Doesn't matter one way or another, but it's a little cleaner that way.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • Duh --- syntactic butchery (my nemesis)! Thanks. – BillB1951 May 09 '12 at 20:51
  • 2
    Except this won't work, because these are instance methods, not class methods. You should call them on `self`, not `Flist`. Although I have no idea why you would want to define a method that simply returns the value of another method. – Daniel Roseman May 09 '12 at 20:56
  • @DanielRoseman: haha I actually self-corrected that in my update but failed to notice it initially. – Chris Pratt May 09 '12 at 20:59
  • See question continuation above... still having problems. – BillB1951 May 09 '12 at 22:27
  • Can I just forget about method and @proprties on the model and just do something in the SearchInexes.py like --- new_used = indexes.CharField(model_attr='self.get_new_used_display()', faceted=True) – BillB1951 May 10 '12 at 02:55
  • I'm a little rusty on Haystack, but I'm pretty sure that should be `model_attr='get_new_used_display'`. – Chris Pratt May 10 '12 at 14:37