Home > Blockchain >  Updating data in a model from views Django
Updating data in a model from views Django

Time:01-13

Hi i have a problem with updating data which is stored in a model. I would like to update the data which is stored in a model without a form, it is a function which sums up every user transaction and after every change I would like it to update.

my models:

class Portfolio(models.Model):
    portfolio_id = models.AutoField(primary_key=True,blank=True)
    portfolio_title = models.CharField(unique=True,max_length=200, null=True,blank=True)
    user_name = models.ForeignKey(Customer, null=True, on_delete=models.SET_NULL,blank=True)
    
    p_shares_num_sum = models.DecimalField(decimal_places=2,default=0,max_digits=999,editable=True, null=True,blank=True)
    p_last_mod_date = models.DateField(auto_now_add=False,null=True,editable=True,blank=True)
    p_comp_num_sum = models.DecimalField(decimal_places=2,default=0,max_digits=999,editable=True, null=True,blank=True)
    p_to_buy_percentage = models.CharField(max_length=200,editable=True, null=True,blank=True)
    p_profit_earned = models.DecimalField(decimal_places=6,editable=True,default=0,max_digits=999, null=True,blank=True)
    
    def __str__(self):
        return self.portfolio_title if self.portfolio_title else ''
    ```
The data which I want to send to it after every entry on site, with function
    shares_num = visdata.aggregate(Sum(('shares_number')))
    shares_num_sum = (shares_num['shares_number__sum'])
    shares_num_sum = format(shares_num_sum, ".0f")
    #profit_earned = visdata.aggregate(Sum(('course')))
    #profit_sum = (profit_earned['course__sum'])
    fare_paid = visdata.aggregate(Sum(('fare')))
    fare_sum = (fare_paid['fare__sum'])

    mod_date = visdata.order_by('-date').first().date
    
    to_buy = visdata.filter(buy_sell=' ').count()
    to_sell = visdata.filter(buy_sell='-').count()

    to_buy_percentage = 0
    to_buy_percentage = to_buy / comp_number
    to_buy_percentage = (to_buy_percentage) * 100
    to_buy_percentage = format(to_buy_percentage, ".0f")
    to_buy_percentage = str(to_buy_percentage)   '%'
    
    #for customer restriction delete object and change VisData to visdata
    aggregated_data = visdata.annotate(
    intermid_result=F('course') - F('fare') 
    ).annotate(
    record_total=F('shares_number') * F('intermid_result')
    ).aggregate(
    total=Sum('record_total')
    )
    profit_earned = aggregated_data['total']
    profit_earned = format(profit_earned, ".2f")

summary_data = [int(shares_num_sum),int(comp_number),mod_date,str(to_buy_percentage),float(profit_earned)]

The function is written and prints me:
[4, 2, datetime.date(2021, 12, 20), '100%', 0.9]

CodePudding user response:

If you have that data from function in your view, just get your current portolio object and asign to it's fields values, then call save() method from that object.

For example:

portfolio_object = Portfolio.objects.get(pk=some_pk)
portfolio_object.some_field = summary_data[0]
... here the rest of values

portfolio_object.save()

Remember that it'll execute every time you open that view, so think about some optimalization.

  •  Tags:  
  • Related