基于Django class的视图,在CreateView之后保存在另一个模型中

Django class based view, save in another model after CreateView

我有一个创建视图 (Loan_assetCreateView(generic.CreateView)),如果要借出资产以及何时将其归还,我会保存在名为 Loan_asset 的模型中(models.Model)。然后我将资产放在不同的模型资产 (model.Model) 中。一旦我将数据保存在我的 Loan_assetCreateView(generic.CreateView) 中,我想将 Asset.is_loaned 中的值设置为 True。我该怎么做?

我的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

您可以覆盖 Loan_assetCreateView 中的 post 方法。

class Loan_assetCreateView(generic.CreateView):
    model = models.Loan_asset
    form_class = forms.Loan_assetForm
    
    def post(request, *args, **kwargs):
        response = super().post(request, *args. **kwargs)
        # Do your thing
        return response
        

这里有一些选项:

  • 重写在 post 方法实现中调用的 form_valid 方法,以便在验证表单(保存模型实例)后,您将能够通过外部设置标志 key/by 创建资产实例:
...

def form_valid(self, form):
    self.object = form.save()

    if self.object.asset:
        self.object.asset.is_loaned = True
    else:
        self.object.asset = Asset.objects.create(is_loaned=True) 
    return HttpResponseRedirect(self.get_success_url())
@receiver(post_save, sender=Loan_asset)
def create_transaction(sender, instance, created, **kwargs):
    if created:
        Asset.objects.create(is_loaned=True)