Beispiel #1
0
    def test_can_search_books_by_categories(self):
        com = Categories()
        com.name = 'Computer'
        com.save()
        math = Categories()
        math.name = 'Math'
        math.save()

        first_book = Book()
        first_book.name = 'Data Structures and Algorithms in Python'
        first_book.score = 3.8
        first_book.save()
        first_book.categories.add(com)

        second_book = Book()
        second_book.name = 'Data Structures and Algorithms with Java'
        second_book.score = 4.0
        second_book.save()
        second_book.categories.add(com)

        third_book = Book()
        third_book.name = 'Discrete Math'
        third_book.score = 4.5
        third_book.save()
        third_book.categories.add(com, math)

        fourth_book = Book()
        fourth_book.name = 'Math II'
        fourth_book.score = 3.0
        fourth_book.save()
        fourth_book.categories.add(math)

        fifth_book = Book()
        fifth_book.name = 'A byte of Python'
        fifth_book.score = 3.5
        fifth_book.save()
        fifth_book.categories.add(com)

        saved_cat = Categories.objects.all()
        self.assertEqual(saved_cat[0], com)
        self.assertEqual(saved_cat[1], math)

        saved_book = Book.objects.all()
        self.assertEqual(saved_book.count(), 5)

        search_book = Book.objects.filter(name__contains="Data")
        self.assertEqual(search_book.count(), 2)

        search_cate = Categories.objects.filter(name__contains="Com")[0]
        self.assertEqual(search_cate.book_set.all().count(), 4)

        book_in_cate = search_cate.book_set.filter(name__contains="Data")
        self.assertEqual(search_book.count(), 2)

        book_in_two_cate = Book.objects.filter(name__contains="Discrete")[0]
        self.assertEqual(book_in_two_cate.categories.all().count(), 2)
Beispiel #2
0
def index(request):
    '''
    操作rom
    1.所有的模型类都有个objects属性,就是一个manager对象,用于进行数据操作管理
    2.添加数据
        2.1 模型类。objects.create(属性=值)
                返回生成的对象----》记录
        2.2 添加数据
            对象 = 模型类()
            对象。属性=值
            对象。save()保存数据
        2.3 ,模型类。objects.get_or_crate(属性=值)
            如果有就获取,没有就创造
    3查看数据
            模型类。objects.all() 查看所有数据
            模型类。objects.filter(条件)条件查询,相当于where
            模型类。objects.get(条件)只能获取一条数据
    4修改数据
            4.1 模型类。object。filter(条件).update(属性=新属性值)
                一次更新多条
            4.2 先查询出对象  ---一次更新一条
                对象。属性=值
                对象。属性=值
                对象。属性=值
                对象。save()
        5 删除数据
            查询偶delete()
    :param request:
    :return:
    '''
    rs = Book.objects.create(name='红楼梦', author='曹雪芹', price=9.9)
    print(rs)
    book = Book()
    book.name = '红楼梦'
    book.author = '曹雪芹'
    book.price = 99
    book.save()
    book = Book.objects.get_or_create(name='三国演义', author='施耐庵', price=88)
    print(book)

    #查看数据
    # books=Book.objects.all()
    # content = {
    #     "books":books
    # }
    # book = Book.objects.get(id=1)
    # book = Book.objects.get(name='红楼梦')  如果存在多条数据,返回列表[]
    # books = Book.objects.filter(name='红楼梦').first()#获取第一条
    # print(book)

    #更新数据
    # rs = Book.objects.filter(name='红楼梦').update(name='红楼2')
    # print(rs)
    # book = Book.objects.get(id=1)
    # book.name='红楼3'
    # book.save()
    book = Book.objects.get(id=1)
    book.delete()
    return HttpResponse("bookModel")
Beispiel #3
0
def book_input_auto():
    book_list = get_data('book_out.txt')
    from book.models import Book
    from book.models import Taxonomy
    
    for b in book_list:
        bs = None
        if b[6] != '':
            bs = Book.objects.filter(isbn=b[6]) 
        if bs is None or not bs.exists():
            bs = Book.objects.filter(name=b[0], version=b[7]) 
            if not bs.exists():
                try:
                    book = Book()
                    book.name = b[0]
                    book.subtitle = b[1]
                    if b[2] is None:
                        book.author = ''
                    else:
                        book.author = b[2]
                    if b[3] is None:
                        book.press = ''
                    else:
                        book.press = b[3]
                    book.price_ori = b[4]
                    if b[5] is None or b[5] == 0:
                        book.price_now = 0.4*b[4]
                    else:
                        book.price_now = b[5]
                    book.isbn = b[6]
                    book.version = b[7]
                    book.publication_date = b[8]
                    book.page_number = b[9]
                    book.size = b[10]
                    book.binding = b[11]
                    if b[12] == 'zh':
                        book.language = 'zh'
                    else:
                        book.language = 'en'
                    if b[13] is None:
                        book.translator = ''
                    else:
                        book.translator = b[13]
                    book.status = 4
                    book.save()
                    try:
                        if b[14] is not None and b[14] != '':
                            t = Taxonomy.objects.get(name=TAXONOMY_DICT[b[13]])
                            book.add_taxonomy(t)
                    except Exception, e:
                        print "Add taxonomy failed"
                        print e
                        print book_list.index(b)
                        print b
                        import pdb;pdb.set_trace()
                except Exception, e:
                    print e
                    print book_list.index(b)
                    print b
Beispiel #4
0
 def add_book(self, json_object):
     book = Book()
     book.name = json_object['name']
     book.save(
     )  # must be saved to assign id (used to establish a many-to-many relationship)
     book.author.set(json_object['author'])
     book.description = json_object['description']
     book.genre.set(json_object['genre'])
     book.user.set(json_object['user'])
     book.save()
Beispiel #5
0
def add_fk1():
    from book.models import Publisher, Book

    p = Publisher.objects.get(name='机械工业出版社')

    b = Book()
    b.name = 'Python 高并发实战'
    b.publisher_state = 2
    b.desc = 'Good'
    b.publisher = p  # ForeignKey
    b.save()
Beispiel #6
0
def create_book(request):
    if request.method == 'POST':
        createbookform = CreateBookForm(request.POST, request.FILES)
        if createbookform.is_valid():
            book = Book()
            book.category = createbookform.cleaned_data['category']
            book.name = createbookform.cleaned_data['name']
            book.cover = createbookform.cleaned_data['cover']
            book.summary = createbookform.cleaned_data['summary']
            book.author = request.user.author
            book.save()
            return redirect(reverse('author:update_book', args=[book.pk]))
        else:
            print(createbookform.errors)
    else:
        createbookform = CreateBookForm()
    context = dict()
    context['createbookform'] = createbookform
    return render(request, 'author_area/manage/create_book.html', context)