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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:48:07+00:00 2026-05-30T03:48:07+00:00

I want to log actions made by users. In most OO languages, I would

  • 0

I want to log actions made by users. In most OO languages, I would implement this via a LoggedAction class, having several child classes like LoginActionand LogoutAction. I could then iterate over a list of LoggedActions and get the specific child behaviour through virtual inheritance. This does not work using Django models however.

Example models.py:

class LoggedAction(models.Model):
    user = models.ForeignKey(User)
    timestamp = models.DateTimeField(auto_now_add=True)

    def __unicode__(self):
        return "%s: %s %s" % (unicode(self.timestamp), unicode(self.user), unicode(self.action()))

    def action(self):
        return ""

class LoginAction(LoggedAction):
    def action(self):
        return "logged in"

class LogoutAction(LoggedAction):
    def action(self):
        return "logged out"

Then I’d like to do [unicode(l) for l in LoggedAction.objects.all()] and get a list of messages like u'2012-02-18 18:47:09.105840: knatten logged in'.

As expected, this does not work, since what I get from all() is a list of LoggedAction objects having either a loginaction member or a logoutaction member. (The output is a list of messages like u'2012-02-18 18:47:09.105840: knatten, with no mention of the action.)

Is there a sane way to get the behaviour I’m after, or am I trying to apply the wrong paradigm here? (I guess I am, and that I should just have the specific action as a member in LoggedAction)

  • 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-30T03:48:09+00:00Added an answer on May 30, 2026 at 3:48 am

    Yes, this is probably the wrong paradigm. It’s easy to be misled by the object-relational mapper (ORM) – database tables don’t really map all that well to objects, and this difference is known as the object-relational impedance mismatch.

    What you actually need is to make action a field. This field can take a choices parameter which represents the possible values of that field – ie logged in or logged out:

    class LoggedAction(models.Model):
        ACTIONS = (
           ('I', 'logged in'),
           ('O', 'logged out')
        )
        user = models.ForeignKey(User)
        timestamp = models.DateTimeField(auto_now_add=True)
        action = models.CharField(max_length=1, choices=ACTIONS)
    
        def __unicode__(self):
            return u"%s: %s %s" % (self.timestamp, self.user, self.get_action_display())
    

    Note that I’ve used arbitrary single-character strings to represent the actions, and the get_action_display() magic method to get the full description.

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

Sidebar

Related Questions

I want to log user's actions in my Ruby on Rails application. So far,
I'm creating a table called news_feed , this will log all the actions a
I want to do some actions when class .smth is clicked $('.smth').click(function() { and
I want to make some actions available only for logged in users. I've tried
i want log linenumber to my log file, using log4net.the config details of log4net
We want to log the user out when the session ends, so one approach
I want to log onto Stack Overflow using OpenID, but I thought I'd set
I want to log to the console when I'm using un-minimized JavaScript files. Comments
I want the log to roll over as long as the application is running,
I want to log my log strings to a file. I implemented the following

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.