S
S
Salavat Sharapov2015-09-17 20:15:54
Django
Salavat Sharapov, 2015-09-17 20:15:54

How to break cycles or how not to fall into recursion?

There are three "great" models:
models.py

class Book(models.Model):
    title = models.CharField(max_length=255)
    text = models.TextField()

class Images(models.Model):
   image = models.ImageField(upload_to=file_uploads_to)
   alt = models.CharField(max_length=255)

class BookImages(models.Model):
    book = models.ForeignKey(Book, related_name='book_images')
    image = models.ForeignKey(Images)

There is views.py
def book_image_set(request, book_id):
    """" request  - приходит GET с листом id's модели Images либо пустой.

    try:
       book = Book.objects.get(id=book_id)
   except Book.DoesNotExist:
       pass
   else:
         .... проверка на то какой метод запроса ...
         ..... и тут пошла суть вопроса.....
         if  book.book_images.all():
             for book_image in book.book_images.all():
                 item = book_image.image
                 if not request.GET.keys():
                    book_image.delete()
                 else:
                    for i in request.GET:
                        if item.id == i:
                            continue
                        if item.id !=i and item.id in request.GET.keys():
                            continue
                        if item.id !=i and item.id not in request.GET.keys():
                            book_image.delete()
                        else:
                            # пропустим try
                            image = Image.obejcts.get(id=i)
                            book_img = BookImages(book=book, image=image)
                            book_img.save()
           else:
                 for i in request.GET:
                     image = Image.obejcts.get(id=i)
                     book_img = BookImages(book=book, image=image)
                     book_img.save()
                        
                # На повтор кода не обращайте внимания.

Four GET.keys() come in turn:
1) [55, 56]
2) [55, 56, 57]
3) [56, 57]
4) []
So, on the second request, the loops do not correctly issue logic. Toli lathered his eyes. That something did not take into account.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sim3x, 2015-09-17
@sim3x

Php smells
Delete, no csrf, no forms
0. Re-read how Form
1 works. What prevents you from attaching pictures to a book?

class Book(models.Model):
    title = models.CharField(max_length=255)
    text = models.TextField()

class Images(models.Model):
   book = models.ForeignKey(Book, related_name='book_images')
   image = models.ImageField(upload_to=file_uploads_to)
   alt = models.CharField(max_length=255)

if the pictures are duplicated
class Book(models.Model):
    title = models.CharField(max_length=255)
    text = models.TextField()

class Images(models.Model):
   book = models.ManyToMany(Book, related_name='book_images')
   image = models.ImageField(upload_to=file_uploads_to)
   alt = models.CharField(max_length=255)

2. What prevents you from filtering pictures by id and deleting them in bulk?
Images.filter(pk__in=[id1, id2,...]).detele()

N
Nikita Konin, 2015-09-17
@jkjkjirf

I apologize for the harshness of expressions, but this is the wildest shit code.
I can’t tell where the error is, because the logic of the work is not at all obvious and it’s not clear what you are trying to do.
Here, for example, the code after else will not be executed under any circumstances.

for i in request.GET:    
    if item.id == i:
        continue
    if item.id !=i and item.id in request.GET.keys():
        continue
    if item.id !=i and item.id not in request.GET.keys():
        book_image.delete()
    else:
        # пропустим try
        image = Image.obejcts.get(id=i)
        book_img = BookImages(book=book, image=image)
        book_img.save()

I advise you to have a good rest, and then re-read some book on python, for example this one, then the documentation on django, after which you will stop asking such questions.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question