tgoop.com/djangolearn_ir/1096
Create:
Last Update:
Last Update:
سوال:
شما اسلایدر برای سایت تون چطوری می سازید؟
این پیشنهاد chatgpt بود:
اگه میخوای اسلایدرت خیلی انعطافپذیر باشه، میتونی از GenericForeignKey استفاده کنی که به هر مدل وصل بشه (محصول، مقاله، دستهبندی و …).
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
class Slider(models.Model):
title = models.CharField(max_length=255, blank=True)
image = models.ImageField(upload_to="sliders/")
url = models.URLField(blank=True, null=True)
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE, null=True, blank=True)
object_id = models.PositiveIntegerField(null=True, blank=True)
content_object = GenericForeignKey('content_type', 'object_id')
order = models.PositiveIntegerField(default=0)
is_active = models.BooleanField(default=True)
مزایا:
نهایت انعطافپذیری: میتونی هر چیزی رو توی اسلایدر بذاری.
آیندهنگر و تمیز.
معایب:
کار باهاش پیچیدهتره.
برای خیلی از پروژههای معمولی زیادی Over-Engineering محسوب میشه.
BY جنگولرن
Share with your friend now:
tgoop.com/djangolearn_ir/1096
