Reventlow
Reventlow

Reputation: 167

Django save via signals in another table

I have a create view (Loan_assetCreateView(generic.CreateView)) where I save if an asset is going to be loaned and when it will be returened in a model called Loan_asset(models.Model). Then I have the asset in a diffrent model Asset(model.Model). I would like to once I have saved my data in my Loan_assetCreateView(generic.CreateView) that is set the value in Asset.is_loaned to True. Via signal, @receiver(post_save, I am trying to save the value. But it creates another asset instead. What am I dong wrong and how can I fix it?

My 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,))

my urls.py

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

my 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)

Upvotes: 0

Views: 296

Answers (1)

Scratch'N'Purr
Scratch'N'Purr

Reputation: 10427

If I'm understanding correctly, you want the associated Asset instance of the Loan_asset instance to have its is_loaned switched to True. If that's the case, then your signal should be:

@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`

Upvotes: 1

Related Questions