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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:01:00+00:00 2026-05-12T08:01:00+00:00

I have a design question concerning Django. I am not quite sure how to

  • 0

I have a design question concerning Django. I am not quite sure how to apply the principle of loose coupling of apps to this specific problem:

I have an order-app that manages orders (in an online shop). Within this order-app I have two classes:

class Order(models.Model):
    # some fields
    def order_payment_complete(self):
        # do something when payment complete, ie. ship products
        pass

class Payment(models.Model):
    order = models.ForeignKey(Order)
    # some more fields     
    def save(self):
        # determine if payment has been updated to status 'PAID'
        if is_paid:
            self.order.order_payment_complete()
        super(Payment, self).save()

Now the actual problem: I have a more specialized app that kind of extends this order. So it adds some more fields to it, etc. Example:

class SpecializedOrder(Order):
    # some more fields
    def order_payment_complete(self):
        # here we do some specific stuff
        pass

Now of course the intended behaviour would be as follows: I create a SpecializedOrder, the payment for this order is placed and the order_payment_complete() method of the SpecializedOrder is called. However, since Payment is linked to Order, not SpecializedOrder, the order_payment_complete() method of the base Order is called.

I don’t really know the best way to implement such a design. Maybe I am completely off – but I wanted to build this order-app so that I can use it for multiple purposes and wanted to keep it as generic as possible.

It would be great if someone could help me out here!
Thanks,
Nino

  • 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-12T08:01:01+00:00Added an answer on May 12, 2026 at 8:01 am

    I think what you’re looking for is the GenericForeignKey from the ContentTypes framework, which is shipped with Django in the contrib package. It handles recording the type and id of the subclass instance, and provides a seamless way to access the subclasses as a foreign key property on the model.

    In your case, it would look something like this:

    from django.db import models
    from django.contrib.contenttypes.models import ContentType
    from django.contrib.contenttypes import generic
    
    class Payment(models.Model):
    
        order_content_type = models.ForeignKey(ContentType)
        order_object_id = models.PositiveIntegerField()
        order = generic.GenericForeignKey('order_content_type', 'order_object_id')
    

    You don’t need to do anything special in order to use this foreign key… the generics handle setting and saving the order_content_type and order_object_id fields transparently:

    s = SpecializedOrder()
    p = Payment()
    p.order = s
    p.save()
    

    Now, when your Payment save method runs:

    if is_paid:
        self.order.order_payment_complete()  # self.order will be SpecializedOrder
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is more of a design question. I have a template class, and I
This is more of a general design question I guess... I have an Ajax
This is probably more a design or style question: I have just been considering
I've been reading Design Patterns, by Gamma et al. I have a question concerning
i just have a little design question. If i got this code public Interface
This is a design question rather than a Java-specific question, but I'm designing it
This is more of a design question that others may have had similar experience
this is more a design question. Suppose you have many beans A, B, C....
This is a tough design question for a application I'm working on. I have
This is more or less a design question. we have to process like 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.