django模型ForeignKey on_delete设置自定义默认

时间:2020-05-11 15:22:41

标签: django django-models django-rest-framework

我有一个模型subscription,其中的类定义如下,

class Subscription(TimestampedModel):
    ...
    notification = models.ForeignKey(
         'settings.NotificationPreference', on_delete=models.SET(get_default_reminder), null=True
    )
    ...

如果要删除外部模型,我想设置default notification: id,经过研究发现,可以使用https://docs.djangoproject.com/en/2.1/ref/models/fields/#django.db.models.SETset方法,我参考了it was using TypeScript's Module Resolution behaviour实施,但是我现在陷入困境,因为默认通知将基于每个用户。我的get_default_reminder函数如下所示,

def get_default_reminder():
    return NotificationPreference.objects.get(user_id=user_id, name='Default Notification')

这里的问题是我不明白如何传递user_id来获取用户特定的默认通知ID。

0 个答案:

没有答案