2021年1月27日星期三

New Entry of Data overwrites all past entries in column , how do i correct this?

Using Django,

when the User fills out the webform, one of the saved Data overwrites all pass entered data for that column. How do i stop it from overwriting it the other past data for that column ?

Models.py file below,

from django.db import models  from django.utils import timezone  from django.contrib.auth.models import User           class ansquestions(models.Model):      m_invested = models.CharField(max_length=100)      p_return = models.CharField(max_length=100)      years = models.CharField(max_length=100)      inflation_yes_no = models.CharField(max_length=100)      date_answered = models.DateTimeField(default=timezone.now)      author = models.ForeignKey(User, on_delete=models.CASCADE, null=True)      results = models.ForeignKey('results', on_delete=models.CASCADE, null=True) # null=True                    def __str__(self):          return self.m_invested + ' ' +self.p_return          class results(models.Model): # second table for output and add other data from ansquestions table as foreign keys      r_output = models.CharField(max_length=500)      author = models.ForeignKey(User, on_delete=models.CASCADE, null=True)      def __str__(self):          return self.r_output      # Important : this dunder method above __str__(self) for r_ouptput was how the actual r_output value gets displayed on admin interface. important since it was foreign key  

Here is the 2nd View Function, This is where i'm having the trouble with the latest.objects('id') command, i found this as a solution, but it's overwriting all of the rows for that "Results" Column

def formulafv(request):       if makeup_infl=='no':          i_return = (float(perc_r))      elif makeup_infl=='yes' and int(years_i)<=5:          i_return = (2+float(perc_r))      elif makeup_infl=='yes' and int(years_i)>5 and int(years_i)<=9.99 :          i_return = (4 + float(perc_r))      elif makeup_infl=='yes' and int(years_i)>= 10 :          i_return = ( 6 + float(perc_r))        fv_i_value = int(total_i) * (1 + (i_return)*.01) ** int(years_i)        r_output = 'Your money would grow to the total of ' + str(fv_i_value) +' dollars at the end of this timeframe.'         if request.method == "POST": # second pre-filled html form for saving r_output. for right now have to press submit button on screen. (i would want this to automatically (save/submit).                tom = ResForm(request.POST or None)          if tom.is_valid():              r_output = request.POST.get("r_output", '')              bamdata = results(r_output=r_output)              bamdata.author = request.user              bamdata.save()                                                     ansquestions.results = results.objects.latest('id')                              dabby = ansquestions.results               dabby.author = request.user               dabby.save()  

enter image description here

https://stackoverflow.com/questions/65930289/new-entry-of-data-overwrites-all-past-entries-in-column-how-do-i-correct-this January 28, 2021 at 10:43AM

没有评论:

发表评论