Answer the question
In order to leave comments, you need to log in
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)
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()
# На повтор кода не обращайте внимания.
Answer the question
In order to leave comments, you need to log in
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)
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)
Images.filter(pk__in=[id1, id2,...]).detele()
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()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question