Django 通过另一个 table 中的信号保存

Django save via signals in another table

我有一个创建视图 (Loan_assetCreateView(generic.CreateView)),如果要借出资产以及何时将其归还,我会保存在名为 Loan_asset 的模型中(models.Model)。然后我将资产放在不同的模型资产 (model.Model) 中。一旦我将数据保存在我的 Loan_assetCreateView(generic.CreateView) 中,我想将 Asset.is_loaned 中的值设置为 True。通过信号,@receiver(post_save, 我正在尝试保存该值。但它创建了另一个资产。我错了什么,我该如何解决?

我的models.py:

class Asset(models.Model):

    # Relationships
    room = models.ForeignKey("asset_app.Room", on_delete=models.SET_NULL, blank=True, null=True)
    model_hardware = models.ForeignKey("asset_app.Model_hardware", on_delete=models.SET_NULL, blank=True, null=True)

    # Fields
    name = models.CharField(max_length=30)
    serial = models.CharField(max_length=30, unique=True, blank=True, null=True, default=None)
    mac_address = models.CharField(max_length=30, null=True, blank=True)
    purchased_date = models.DateField(null=True, blank=True)
    may_be_loaned = models.BooleanField(default=False, blank=True, null=True)
    is_loaned = models.BooleanField(default=False, blank=True, null=True)
    missing = models.BooleanField(default=False, blank=True, null=True)
    notes = HTMLField(default="")
    ip = models.CharField(max_length=90, null=True, blank=True)
    created = models.DateTimeField(auto_now_add=True, editable=False)
    last_updated = models.DateTimeField(auto_now=True, editable=False)

class Loan_asset(models.Model):

    # Relationships
    asset = models.ForeignKey("asset_app.Asset", on_delete=models.SET_NULL, blank=True, null=True)
    loaner_type = models.ForeignKey("asset_app.Loaner_type", on_delete=models.SET_NULL, blank=True, null=True)
    location = models.ForeignKey("asset_app.Locations", on_delete=models.SET_NULL, blank=True, null=True)

    # Fields
    loaner_name = models.CharField(max_length=60)
    loaner_address = models.TextField(max_length=100, null=True, blank=True)
    loaner_telephone_number = models.CharField(max_length=30)
    loaner_email = models.EmailField()
    loaner_quicklink = models.URLField(null=True, blank=True)
    loan_date = models.DateField()
    return_date = models.DateField()
    notes = HTMLField(default="")
    returned = models.BooleanField(default=False, blank=True, null=True)
    created = models.DateTimeField(auto_now_add=True, editable=False)
    last_updated = models.DateTimeField(auto_now=True, editable=False)
    class Meta:
        pass

    def __str__(self):
        return str(self.loaner_name)

    def get_absolute_url(self):
        return reverse("asset_app_loan_asset_detail", args=(self.pk,))

    def get_update_url(self):
        return reverse("asset_app_loan_asset_update", args=(self.pk,))

我的urls.py

`path("asset_app/loan_asset/create/", views.Loan_assetCreateView.as_view(), name="asset_app_loan_asset_create")`,

我的views.py

class Loan_assetCreateView(generic.CreateView):
model = models.Loan_asset
form_class = forms.Loan_assetForm

def get_queryset(self):
    queryset = super().get_queryset().order_by('asset__model_hardware__asset_type', 'asset.name')
    return queryset

@receiver(post_save, sender=models.Loan_asset)
def create_transaction(sender, instance, created, **kwargs):
    if created:
        models.Asset.objects.create(is_loaned=True)

如果我理解正确,您希望 Loan_asset 实例的关联 Asset 实例将其 is_loaned 切换为 True。如果是这样,那么您的信号应该是:

@receiver(post_save, sender=models.Loan_asset)
def create_transaction(sender, instance, created, **kwargs):
    if created:
        asset = instance.asset
        asset.is_loaned = True
        # asset.save()  # save entire instance, overwriting `last_updated` OR
        asset.save(update_fields=["is_loaned"])  # updates only `is_loaned`