考虑以下模型:
class Request(models.Model):
user = models.ForeignKey('User', related_name='requests')
带有create / update的viewSet:
class RequestViewSet(mixins.CreateModelMixin, mixins.UpdateModelMixin, viewsets.GenericViewSet):
我想仅在“创建”而非“更新”
上设置用户第一个解决方案:
class RequestViewSet(mixins.CreateModelMixin, mixins.UpdateModelMixin, viewsets.GenericViewSet):
def pre_save(self, obj):
create = False
try:
if not obj.user:
create = True
except ObjectDoesNotExist:
create = True
if create:
obj.user = self.request.user
使用restore_object的第二个解决方案:
class RequestSerializer(serializers.ModelSerializer):
def restore_object(self, attrs, instance=None):
create = not instance
instance = super(RequestSerializer, self).restore_object(attrs, instance)
if create:
request = self.context['request']
instance.user = request.user
return instance
什么更符合逻辑? 我倾向于选择第一个(除了try / except,但我找不到它的替代品)
答案 0 :(得分:1)
我不是基于类的观点的忠实粉丝,我更喜欢胖模型和瘦视图。
我会在Request上选择post_save信号并检查创建的参数。
https://docs.djangoproject.com/en/dev/ref/signals/#post-save
Request(...):
@classmethod
def post_save(cls, sender, instance, created, raw, **kwargs):
if raw:
return
if created:
pass # do your on create stuff here
post_save.connect(Request.post_save, sender=Request, dispatch_uid="uniqueid") # use weak=False if you use a local function
另请注意,instance.save()会再次触发保存信号,因此如果您想避免再次处理所有信号(或者更糟糕的是,创建一个信号循环,您可能会考虑使用.update())。 - ))