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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:11:20+00:00 2026-05-20T10:11:20+00:00

I have a model that I overrode the save method for, so that the

  • 0

I have a model that I overrode the save method for, so that the save method can be passed in some data and auto-fill-in a field before saving. Here is my model:

class AccountModel(models.Model):

    account = models.ForeignKey(Account)

    def save(self, request=None, *args, **kwargs):
        if request:
            self.account = request.session['account']
        super(AccountModel, self).save(*args, **kwargs)

    class Meta:
        abstract = True

The idea is I set up a base model for objects that need to be associated with an account and then I won’t have to deal with the account connections every time they come up (which is a lot).

But: I’d also like to use get_or_create, which saves the new objects without passing in the request. I know it is doable to not use get_or_create and do a try/except instead, but I’d like to know if there is a way to override get_or_create and what is the proper way to do it.

I looked at the code for the Manager (which I am looking at overriding) and the get_or_create function just calls a QuerySet.get_or_create function. Maybe I can write it to use other manager functions and not the QuerySet version of get_or_create?
What do y’all think?

  • 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-20T10:11:21+00:00Added an answer on May 20, 2026 at 10:11 am

    You could subclass django.db.models.query.QuerySet and override the get_or_create method there to accept your request keyword argument and pass it onto save I guess, but it isn’t very pretty.

    class AccountQuerySet(models.query.QuerySet):
        def get_or_create(...):
            ...
    

    You could then add a custom manager to your Account model which uses this custom QuerySet:

    class AccountManager(models.Manager):
        def get_queryset(self):
            return AccountQuerySet(self.model)
    

    Then use this manager in your model:

    class Account(models.Model):
        ...
        objects = AccountManager()
    

    But you might find that the try-except method is neater after all 🙂

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

Sidebar

Related Questions

How can I override the IsValid property? I have a model that's validating as
I have a model that uses a acts_as_nested_set fork, and I've added a method
I have a model that contains a boolean field representing the item's approval or
Problem I want to save the attributes of a model that have changed when
I have a model that looks like this: Performance - Location - Event -
I have a model Articles that has_many Assets which is a polymorphic model that
I have a model that contains an Address property and Latitude / Longitude properties.
I have a model that requires quite a few BooleanFields, they could be considered
I have a Model that named word. this is my word model public class
I have a model that looks like: [Serializable] public class TemplatePageModel { public PageModel

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.