Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9171211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:07:55+00:00 2026-06-17T16:07:55+00:00

models.py class KEBReading(models.Model): #id = models.AutoField(primary_key=True,db_column=’kr_id’) date=models.DateField() time=models.TimeField() truepower_reading=models.DecimalField(max_digits=6, decimal_places=2,blank=False,null=False) apparentpower_reading=models.DecimalField(max_digits=6, decimal_places=2,blank=False,null=False) truepower_consumed=models.DecimalField(max_digits=6, decimal_places=2)

  • 0

models.py

class KEBReading(models.Model):
    #id = models.AutoField(primary_key=True,db_column='kr_id')
    date=models.DateField()
    time=models.TimeField()
    truepower_reading=models.DecimalField(max_digits=6,
    decimal_places=2,blank=False,null=False)
    apparentpower_reading=models.DecimalField(max_digits=6,
    decimal_places=2,blank=False,null=False)
    truepower_consumed=models.DecimalField(max_digits=6, decimal_places=2)
    voltage_reading=models.IntegerField(blank=False,null=False)
    powerfactor=models.DecimalField(max_digits=3,decimal_places=2)

forms.py

class KEBReading_form(forms.ModelForm):
    class Meta:
        model=KEBReading

        exclude=("truepower_consumed","powerfactor")
        widgets = {

               "date" : forms.widgets.DateInput(format="%d/%m/%Y"),
               "time" : forms.widgets.TimeInput(format="%H:%M")
               }

views.py

def KEBReading1(request):
    if request.method == "POST":


        form = KEBReading_form(request.POST)
        if form.is_valid():
            prevdate=KEBReading.objects.latest("date")

            print prevdate.date
            print prevdate.time
        # q1 = KEBReading.objects.get(datetime.date.today()-datetime.timedelta(0))

            kr_truepower_reading = form.cleaned_data["truepower_reading"]
            kr_apparentpower_reading = form.cleaned_data["apparentpower_reading"]
            truepower_consumed1=kr_truepower_reading-prevdate.truepower_reading
            powerfactor1=kr_truepower_reading-                                           prevdate.truepower_reading/(kr_apparentpower_reading-prevdate.apparentpower_reading)


            form.save()
            KEBReading.objects.filter().values("date","time","truepower_reading",
            "apparentpower_reading","truepower_consumed","voltage","powerfactor")
            q2=KEBReading.objects.latest("date")

            context={'KEBReading_form':form,'q2':q2}
            return                  render_to_response('keb.html',context,context_instance=RequestContext(request))

        else:
            form = KEBReading_form()

            return render_to_response('keb.html',{'KEBReading_form':form},context_instance=RequestContext(request))

i want to save the truepower_consumed aftr calculation in the database. but it gives me a error truepower_consumed column cannot be null. i have excluded truepower_consumed in the forms. but in the database i need to save that field

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:07:56+00:00Added an answer on June 17, 2026 at 4:07 pm

    You need to save form with commit=False and assign appropriate values to required fields and save the object again.

    Example:

    def KEBReading1(request):
        if request.method == "POST":
        ....
        #your code till save
        obj = form.save(commit=False)
        #assing your values
        obj.truepower_consumed = truepower_consumed1
        obj.powerfactor = powerfactor1
        obj.save() #save
    

    More details at Using a subset of fields on the form

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

You have models: class Order(Model): date = DateField(editable = False, auto_now_add=True) status = CharField(max_length
models: class Band(models.Model): B_Name = models.CharField(max_length=30, primary_key=True) Country = models.CharField(max_length=30) genre = models.ForeignKey(Genre) imageband
I have theses models: class Year(models.Model): name = models.CharField(max_length=15) date = models.DateField() class Period(models.Model):
Models: class Item(models.Model): name = models.CharField(max_length=100) # More stuff. class Sale(models.Model): sale_date = models.DateField(auto_now_add=True)
I have my models: class Volume(models.Model): volnumber = models.IntegerField('Volume Number', primary_key=True) year = models.TextField()
I have 2 Django models : class A(models.Model): uniq_name = models.CharField(max_length=30,primary_key=True) info1 = models.CharField(max_length=30)
I have the following models: class ProjectUser(models.Model): categories = models.ManyToManyField('UserCategory', blank=True, null=True) user_id =
My models: class Entry(models.Model): title = models.CharField(_('Title'), max_length=200) content = models.TextField(_('Content')) created_at = models.DateTimeField(auto_now_add=True)
I have the following models: class Look(Base): __tablename__ = looks id = Column(Integer, primary_key=True)
Consider the models: class Author(models.Model): name = models.CharField(max_length=200, unique=True) class Book(models.Model): pub_date = models.DateTimeField()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.