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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:27:58+00:00 2026-06-06T04:27:58+00:00

edit: I completely rewrote the question as the original one didn’t clearly explain my

  • 0

edit: I completely rewrote the question as the original one didn’t clearly explain my question

I want to run a function which is specific to each particular model instance.

Ideally I want something like this:

class MyModel(models.Model):
    data = models.CharField(max_length=100)
    perform_unique_action = models.FunctionField() #stores a function specific to this instance

x = MyModel(data='originalx', perform_unique_action=func_for_x)
x.perform_unique_action() #will do whatever is specified for instance x

y = MyModel(data='originaly', perform_unique_action=func_for_y)
y.perform_unique_action() #will do whatever is specified for instance y

However there is no datatype FunctionField. Normally this would be solvable with inheritance, and creating subclasses of MyModel, maybe like this:

class MyModel(models.Model):
    data = models.CharField(max_length=100)
    perform_unique_action = default_function

class MyModelX(MyModel):
    perform_unique_action = function_X

class MyModelY(MyModel):
    perform_unique_action = function_Y

x = MyModelX(data='originalx')
x.perform_unique_action() #will do whatever is specified for instance x

y = MyModelY(data='originaly')
y.perform_unique_action() #will do whatever is specified for instance y

Unfortunately, I don’t think I can use inheritance because I am trying to access the function this way:

class MyModel(models.Model):
    data = models.CharField(max_length=100)
    perform_unique_action = default_function

class SecondModel(models.Model):
    other_data = models.IntegerField()
    mymodel = models.ForeignKey(MyModel)

secondmodel = SecondModel.objects.get(other_data=3)
secondmodel.mymodel.perform_unique_action()

The problem seems to be that I don’t know what type the foreign key is going to be in SecondModel if I override the perform_unique_action in subclasses.

Can I access MyModel from SecondModel as a foreign key and still have a unique function for each instance of MyModel?

  • 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-06T04:27:59+00:00Added an answer on June 6, 2026 at 4:27 am

    This works for me. I haven’t tested it, but you should be able to create another class and override their methods and it’ll work. Check the class Meta line, it’ll treat it as an abstract class. Here’s an example of my actual classes that I’m working on right now.

    EDIT: Added VoteComment class and tested it. It works as expected!

    class Vote(models.Model):
        VOTE_ENUM = (
            (VoteEnum.DOWN_VOTE, VoteEnum.toString(VoteEnum.DOWN_VOTE)),
            (VoteEnum.NONE, VoteEnum.toString(VoteEnum.NONE)),
            (VoteEnum.UP_VOTE, VoteEnum.toString(VoteEnum.UP_VOTE)),
    
        )
        question = models.ForeignKey(Question, null=False, editable=False, blank=False)
        voter = models.ForeignKey(User, blank=False, null=False, editable=False)
        vote_type = models.SmallIntegerField(default=0, null=False, blank=False, choices=VOTE_ENUM)
    
        class Meta:
            abstract = True
    
        def is_upvote(self):
            return self.vote_type > 0
        def is_downvote(self):
            return self.vote_type < 0
    
    class VoteAnswer(Vote):
        answer = models.ForeignKey(Answer, null=False, editable=False, blank=False)
    
        class Meta:
            unique_together = (("voter", "answer"),) # to prevent user from voting on the same question/answer/comment again
    
        def __unicode__(self):
            vote_type = "UP" if vote_type > 0 else ("DOWN" if vote_type < 0 else "NONE")
            return u"{0}: [{1}] {2}".format(user.username, vote_type, answer.text[:32])
    
        def is_upvote(self):
            return "FOO! "+str(super(VoteAnswer, self).is_upvote())
    
    class VoteComment(Vote):
        comment = models.ForeignKey(Comment, null=False, editable=False, blank=False)
    
        class Meta:
            unique_together = (("voter", "comment"),) # to prevent user from voting on the same question/answer/comment again
    
        def __unicode__(self):
            vote_type = "UP" if vote_type > 0 else ("DOWN" if vote_type < 0 else "NONE")
            return u"{0}: [{1}] {2}".format(user.username, vote_type, comment.text[:32])
    
        def is_upvote(self):
            return "BAR!"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT: I have completely changed the question. I want to use Amazon S3 for
Edit: The question in one line: How to pass along context from a MenuItem
EDIT: I completely reworked this question to reflect my better understanding of the problem
EDIT : please note: i completely re-explained my question. I have application with two
EDIT : I completely re-wrote the question since it seems like I was not
I need to edit or completely replace outline data (bezier curves) of OpenType fonts.
EDIT: Problem solved... and unrelated to the phrasing of the question... sorry for the
Edit: 2020 update -- Please disregard the question below as Google's weather API is
EDIT BEFORE YOU READ: Sorry.. I didn't add newline so it appeared jumbled, I
Edit: Answered my own question. See below. -_- I have a variable defined in

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.