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?