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 8640655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:19:59+00:00 2026-06-12T11:19:59+00:00

I’d like to add at runtime methods to my django model, so that it’s

  • 0

I’d like to add at runtime methods to my django model, so that it’s viewable via the admin-interface. Sense: the user should be able to add an CalculationRule and a “virtual” table occurs with the applied rule.

# model.py
from django.db import models
import types

def rule(self):
  return 12 * self.calorific_requirement

class BuildingTable(models.Model):
    building_nr = models.IntegerField()
    square_meter = models.FloatField()
    calorific_requirement = models.FloatField()

    @staticmethod
    def __new__(cls, *args, **kwargs):
      setattr(cls, "rule", types.MethodType(rule, cls))
      return models.Model.__new__(cls, *args, **kwargs)

    def reqirement_per_m2(self):  # this is also a rule!
      return self.calorific_requirement / self.square_meter

    def __unicode__(self):
        return "Building " + str(self.building_nr)

class CalculationRule(models.Model):
    name = models.CharField(max_length=200)
    rule = models.CharField(max_length=200)

    def __unicode__(self):
        return str(self.name)

But if I add “rule” to my admin.py it raises BuldingTableAdmin.list_display[4], 'rule' is not a callable or an attribute of 'BuldingTableAdmin' or found in the model 'BuildingTable'.

# admin.py
from calccols_app.models import BuildingTable, CalculationRule
from django.contrib import admin

class BuldingTableAdmin(admin.ModelAdmin):
  list_display = ["building_nr", "square_meter", "calorific_requirement", "reqirement_per_m2", "rule"]

admin.site.register(BuildingTable, BuldingTableAdmin)
admin.site.register(CalculationRule)

I’ve tested BuildingTable in django’s shell; the rule-method exists, but doesn’t work properly. Any ideas how I can inject methods, so that django can handle/register them?

  • 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-12T11:20:00+00:00Added an answer on June 12, 2026 at 11:20 am

    You do not need to create fields in your model to display it in the admin. You can do it in ModelAdmin entity.

    class UserAdmin(admin.ModelAdmin):
        list_display = ['username', 'fullname']
    
        def fullname(self, model):
            return  model.first_name + " " + model.second_name
    

    Although we can add a method to your model dynamically. But you must take it as a method, not a property:

    def _status(invite):
        if invite.is_activated:
            return "Activated"
        if invite.is_alive:
            return "Live"
        return "Expired"
    
    _status.short_description = 'status'
    Invite.status = _status # Invite is model class
    

    This “property” is normally displayed in the admin and Django-templates.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to run a str_replace or preg_replace which looks for certain words
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a

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.