2
\$\begingroup\$

I have some question because in my project I have 4 models, and one of models is parent for other 3 and they are connected to parent by foreign key. Then I have view which is used for automatically fill form of those models and save them in database, this is post function in my view:

    def post(self, request, *args, **kwargs):
        self.object = None
        form_2 = self.form_class_search(self.request.POST)
        if form_2.is_valid():
            keyword = form_2.cleaned_data['keyword']
            books = self.search_for_books(keyword)

            if books:
                for book in books:
                    title = self.add_title(book)
                    published_date = self.add_published_date(book)
                    pages = self.add_pages(book)
                    language = self.add_language(book)
                    form = self.form_class_book(
                        {'title': title,
                         'published_date': published_date,
                         'pages': pages,
                         'language': language}
                    )
                    if form.is_valid() and form.cleaned_data["title"] not in Book.objects.values_list('title', flat=True):
                        self.object = form.save()

                        small_thumbnail = self.add_small_thumbnail(book)
                        thumbnail = self.add_thumbnail(book)
                        form_thumbnail = self.form_class_thumbnail(
                            {'small_thumbnail': small_thumbnail,
                                'thumbnail': thumbnail,
                                'book': self.object.id}
                        )
                        if form_thumbnail.is_valid():
                            form_thumbnail.save()

                        if book['volumeInfo'].get('authors'):
                            for author in book['volumeInfo']['authors']:
                                author = self.add_author(author)
                                form_author = self.form_class_author(
                                    {'author': author,
                                        'book': self.object.id}
                                )
                                if form_author.is_valid():
                                    form_author.save()

                        if book['volumeInfo'].get('industryIdentifiers'):
                            for identifier in book['volumeInfo']['industryIdentifiers']:
                                identifier_type = self.add_identifier_type(identifier)
                                identifier = self.add_identifier(identifier)
                                form_industryidentifier = self.form_class_identifier(
                                    {'type': identifier_type,
                                        'identifier': identifier,
                                        'book': self.object.id}
                                )
                                if form_industryidentifier.is_valid():
                                    form_industryidentifier.save()

Code works exactly how I want and for me is also transparent and easy to understand but its quite long and I'm courious if my current code is good as it is now or maybe there is good way to make it shorter.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.