瀏覽代碼

Add `search_fields` to `AbstractAddress` model

Similarly to `base_fields` and `hash_fields` it will be easier to
override fields for the search text.

Without this change to add/remove fields from search text we need to
override the `_update_search_text` method.
master
Basil Dubyk 3 年之前
父節點
當前提交
f067c582c2
共有 2 個檔案被更改,包括 8 行新增7 行删除
  1. 4
    7
      src/oscar/apps/address/abstract_models.py
  2. 4
    0
      tests/integration/address/test_models.py

+ 4
- 7
src/oscar/apps/address/abstract_models.py 查看文件

@@ -235,10 +235,11 @@ class AbstractAddress(models.Model):
235 235
         on_delete=models.CASCADE,
236 236
         verbose_name=_("Country"))
237 237
 
238
-    #: A field only used for searching addresses - this contains all the
239
-    #: relevant fields.  This is effectively a poor man's Solr text field.
238
+    # A field only used for searching addresses - this contains all the
239
+    # `search_fields`.  This is effectively a poor man's Solr text field.
240 240
     search_text = models.TextField(
241 241
         _("Search text - used only for searching addresses"), editable=False)
242
+    search_fields = ['first_name', 'last_name', 'line1', 'line2', 'line3', 'line4', 'state', 'postcode', 'country']
242 243
 
243 244
     # Fields, used for `summary` property definition and hash generation.
244 245
     base_fields = hash_fields = ['salutation', 'line1', 'line2', 'line3', 'line4', 'state', 'postcode', 'country']
@@ -295,11 +296,7 @@ class AbstractAddress(models.Model):
295 296
                     {'postcode': [msg]})
296 297
 
297 298
     def _update_search_text(self):
298
-        search_fields = filter(
299
-            bool, [self.first_name, self.last_name,
300
-                   self.line1, self.line2, self.line3, self.line4,
301
-                   self.state, self.postcode, self.country.name])
302
-        self.search_text = ' '.join(search_fields)
299
+        self.search_text = self.join_fields(self.search_fields, separator=' ')
303 300
 
304 301
     # Properties
305 302
 

+ 4
- 0
tests/integration/address/test_models.py 查看文件

@@ -210,6 +210,10 @@ class TestUserAddress(TestCase):
210 210
             "Terry Barrington, 75 Smith Road, N4 8TY, UNITED KINGDOM",
211 211
             a.summary)
212 212
 
213
+    def test_search_text_value(self):
214
+        a = factories.UserAddressFactory()
215
+        self.assertEqual("Barry Barrington 1 King Road London SW1 9RE UNITED KINGDOM", a.search_text)
216
+
213 217
 
214 218
 VALID_POSTCODES = [
215 219
     ('GB', 'N1 9RT'),

Loading…
取消
儲存