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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:32:30+00:00 2026-06-01T04:32:30+00:00

I am working with a Django model that looks like this: class Subscription(models.Model): user

  • 0

I am working with a Django model that looks like this:

class Subscription(models.Model):
    user = models.OneToOneField(User)

That is, it has a one-to-one association with the User class from Django’s auth module. This association is not optional; however, thanks to some legacy code and some manual tinkering with the database, there are cases in which a user does not have an associated subscription, which means that code like this:

sub = user.subscription

will throw a DoesNotExist exception. However, much of the codebase assumes that every user has an associated Subscription object.

Is there a way I could subclass OneToOneField such that, if the associated Subscription object does not exist in the database, I will create one and return it when user.subscription is called, instead of throwing an exception?

  • 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-01T04:32:31+00:00Added an answer on June 1, 2026 at 4:32 am

    The correct way to do this is to catch the post_save signal, creating an object if necessary.

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

Sidebar

Related Questions

I'm working on some Django-code that has a model like this: class Status(models.Model): code
I want to have a django model like this: class Model(models.Model): string = models.CharField()
I have an extended UserProfile model in django: class UserProfile(models.Model): user = models.ForeignKey(User, unique=True)
I've got a couple django models that look like this: from django.contrib.sites.models import Site
I'm working with Django 1.3, with the following 2 models: class Person(models.Model): name =
1] I have the following model: class UserReportedData(db.Model): #country selected by the user, this
class Story(db.Model): title = db.StringProperty(required=True) slug = db.StringProperty(required=True) I'm working with Django's forms on
Django-admin is pluralizing a model that I have running as a proxy class. The
I have a class that stores information about a week: from django.db import models
Working with Django docs' sample Blog and Entry models, how would one get 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.