我正在嘗試爲一隊卡車實施地理圍欄。我必須將一系列邊界與車輛聯繫起來。除此之外,其中一項要求是保留所有內容,即使一旦刪除以用於審計目的。因此,我們必須對所有內容實施軟刪除。這是問題所在。我的許多字段不符合軟刪除管理器,它包含查找數據集中的活動記錄和非活動記錄。如何過濾多對多字段的模型?
class Vehicle(SoftDeleteModel):
routes = models.ManyToManyField('RouteBoundary', through='VehicleBoundaryMap', verbose_name=_('routes'),
limit_choices_to={'active': True})
class VehicleBoundaryMap(SoftDeleteModel):
vehicle = models.ForeignKey(Vehicle, verbose_name="vehicle")
route_boundary = models.ForeignKey(RouteBoundary, verbose_name="route boundary")
# ... more stuff here
alive = SoftDeleteManager()
class SoftDeleteManager(models.Manager):
use_for_related_fields = True
def get_queryset(self):
return SoftDeleteQuerySet(self.model).filter(active=True)
正如你看到上面我試圖以確保默認管理器是一個軟刪除管理器(即過濾有效記錄只),並嘗試使用極限limit_choices_to但轉出到外地外國模型只不我想要的「通過」模式。如果您有任何建議或建議,我很樂意聽取您的意見。
謝謝!
不要自己實施軟刪除,而應使用類似[django-reversion](https://github.com/etianen/django-reversion)的東西。 – Anonymous
那軟刪除已經到處都已經實現了,我無法再改變它了。我希望我們會使用逆轉,它會爲我們節省很多頭痛。 –