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()
# На повтор кода не обращайте внимания.
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()
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()