繁体   English   中英

Django和Python AttributeError:“零售商”对象没有属性

[英]Django & Python AttributeError: 'Retailer' object has no attribute

在我的测试文件中:

class TestMakeSoup(TestCase):
    fixtures = ['deals_test_data.json']

    def test_string_is_valid(self):
        s = Retailer.objects.get(pk=1)
        with open('/home/danny/PycharmProjects/askarby/deals/tests/BestBuyTest.html', 'r') as myfile:
            text = myfile.read().replace('\n', '')
    self.assertTrue(s.make_soup(text))

在文件中进行测试:

class retailer():
    '''
    Retail site, drawn from database queryset object
    '''
    def __init__(self,r_object):
        '''
        Initializes retailer variables
        obj -> nonetype

        Precondition: r_object.currency == 3
        Precondition: r_object.name != None
        '''
        assert len(r_object.currency) == 3, "{} must be a three-letter string (eg 'USD').".format(r_object.currency)
        assert r_object.name != None, "Name must exist."
        assert r_object.deal_container_css != None, "Title css must exist."
        assert r_object.title_css != None, "Title css must exist."
        assert r_object.price_css != None, "Price css must exist."
        self.name = r_object.name
        self.base_url = r_object.base_url
        self.currency = r_object.currency
        #dict containing css lookup values for various fields
        self.css = {}
        self.css['container'] = self.extract_css(r_object.deal_container_css)
        self.css['title'] = self.extract_css(r_object.title_css)
        self.css['product_model'] = self.extract_css(r_object.product_model_css)
        self.css['price'] = self.extract_css(r_object.price_css)
        self.css['old_price'] = self.extract_css(r_object.old_price_css)
        self.css['brand'] = self.extract_css(r_object.brand_css)
        self.css['image'] = self.extract_css(r_object.image_css)
        self.css['description'] = self.extract_css(r_object.description_css)
        self.css['exclude'] = self.extract_css(r_object.exclude_css)
        self.css['shipping'] = self.extract_css(r_object.shipping_css)

        #dict containing associated clearance urls for retailer
        self.clearance_urls = self.get_clearance_urls()

        #dict to house final list of deals
        self.deals = {}


    def __str__(self):
        return self.name

    def make_soup(self, text):
        assert isinstance(text,str), "text must be string."
        soup = bs4.BeautifulSoup(text, "html.parser")
        if soup:
            return soup
        return False

Retailer呼叫是指我的交易应用中的Retailer模型。 我收到此错误:

Error
Traceback (most recent call last):
  File "/home/danny/PycharmProjects/askarby/deals/tests/test_deals.py", line 101, in test_string_is_valid
    self.assertTrue(s.make_soup(text))
AttributeError: 'Retailer' object has no attribute 'make_soup'

为什么make_soup没有作为方法运行?

零售商类采用从数据库中检索到的对象。 我从数据库中检索了该对象,但没有使用它创建一个类。

def test_makesoup(self):
    z = Retailer.objects.get(pk=1)
    s = dealscan.retailer(z)
    with open('/home/danny/PycharmProjects/askarby/deals/tests/BestBuyTest.html', 'r') as myfile:
        text = myfile.read().replace('\n', '')
    self.assertTrue(s.make_soup(text))

解决它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM