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

  • SEARCH
  • Home
  • 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 6918265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:50:11+00:00 2026-05-27T09:50:11+00:00

I have a deal model that contains two date field. First one is start_date

  • 0

I have a deal model that contains two date field. First one is start_date and the other one is end_date. My aim is when I save a deal I want to update a readonly field which shows the substraction of these two date_field.

I try to write a widget however I can only get one of the fields. Here is my widget:

class DueToWidget(AdminDateWidget):                                                                                                                                                                        
    def render(self,name,value,attrs=None):                                                                                                                                                                
        from datetime import timedelta                                                                                                                                                                     
        output = []                                                                                                                                                                                        
        output.append(super(AdminDateWidget, self).render(name,value,attrs))                                                                                                                               
        if value:                                                                                                                                                                                          
            due_to = value + timedelta(days=1)                                                                                                                                                             
            output.append(u'<p>Diff : %s</p>' % due_to)                                                                                                                                   

        return mark_safe(u''.join(output))

I’m adding one day to the selected date, how can I get the other field’s value ? Or is there any other way to do this ?

  • 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-05-27T09:50:11+00:00Added an answer on May 27, 2026 at 9:50 am

    If you don’t mind having to refresh to see the diff (that is, you only see it after you save the model), then an easier approach is to add a readonly field in the admin, that points to a function, like this:

    class MyModelAdmin(ModelAdmin):
        readonly_fields = ('dates_difference',)
    
        #add your other fields, or put it in a fieldset
        fields = ('dates_difference',)
    
        def dates_difference(self, model_instance):
            return model_instance.end_date - model_instance.start_date
    

    Since your goal is just to display extra information in the model’s admin this is the place to put the code, not in a field’s widget or the model’s class.

    As the readonly_fields documentation specifies, its behavior is nearly identical as the list_display, that is you can point it to attributes on both the model and the model’s admin, and also to callables and methods.

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

Sidebar

Related Questions

I have a data model that contains a DateTime attribute, date. I want to
I want to have one model & view that is served by multiple controllers
I have a model that I overrode the save method for, so that the
I have a Promotion model that contains a promotion codes and a foreignkey to
I have a field in a model that I want users to feel like
I have a model that for edit/update actions only is logically split into two
I often have to deal with XML documents that contain namespaced elements, but doesn't
I have a Model User from Devise with that relations: user.rb # Relationships references_many
I have a form that contains the data to create a Product object. The
Say you have a Book model that can belong to multiple categories. Categories are

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.