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

  • Home
  • SEARCH
  • 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 8476927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:18:49+00:00 2026-06-10T18:18:49+00:00

It’s quite easy to run into import problems with Django and apps which interact

  • 0

It’s quite easy to run into import problems with Django and apps which interact with each other. My question is simple:

What is the accepted process for minimizing circular imports or has anyone come up with an accepted coding standard to reduce these which they are willing to share.?

I’m looking for good principles that can be standardized on.

Models

class Program(models.Model):
    group = models.ForeignKey(Group, related_name="%(app_label)s_%(class)s_related") 

vs

class Program(models.Model):
    group = models.ForeignKey('auth.Group', related_name="%(app_label)s_%(class)s_related") 

Views:

class ProgramDetailView(DetailView):
    """Detail view of the EEP Program"""

    def get_queryset(self):
        """Narrow this based on your company"""
        from apps.company.models import Company
        company = Company.objects.get(name="foo")
        return Program.objects.filter(company = company)

vs (which tends to cause the issues..

from apps.company.models import Company
class ProgramDetailView(DetailView):
    """Detail view of the EEP Program"""

    def get_queryset(self):
        """Narrow this based on your company"""
        company = Company.objects.get(name="foo")
        return Program.objects.filter(company = company)

The problem with this is that you tend to do a lot of imports all over the place..

  • 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-10T18:18:51+00:00Added an answer on June 10, 2026 at 6:18 pm

    Over the years I standardized on some patterns, based on my observations on
    how I develop web apps.

    I don’t know what are your standards about modularity and code re-use but the
    following simple rules/patterns have helped me greatly with some fairly large
    projects.

    I have noticed that many of my models share some common attributes. For example
    I prefer to use UUID’s instead of simple auto increment integers, as primary keys.

    So I have this abstract model.

    class UUIDModel(models.Model):
        id = UUIDField(primary_key=True, auto=True)  # There are many implementation of this on the web. Choose your favorite.
    
        class Meta:
            abstract = True
    

    Many of my models need the the concept of activation. So I have another abstract model,
    similar to this:

    class ActivatedModel(Model):
        is_active = models.BooleanField(default=False)
    
        def activate(self, save=True):
            if self.is_active:
                raise Exception('Already activated')
            self.is_active = True
            if save:
                self.save()
    
        class Meta:
            abstract = True
    

    There are many other abstract models I use for tracking creation time and modification, or
    if something is finalized and can’t be further modified, etc.

    All these abstract models live in the core app. This is how I call it.
    Apart from the core app, I have a tasks app. The tasks app offers abstract
    models that augments any interfacing I have to do with celery which I use a lot.

    The tasks app can import models from the core app, but not the other way around.

    I have also an mms app, which handles Multimedia creation and transformations(thumbnails, etc). The mms can import models from the previous apps. So the import relationship we have right now is this: core -> tasks -> mms.

    Every other app I create is specific to the current project I am working on and builds upon
    the previous apps. So basically I try to have “one way imports” if you can call it that.

    And I end up with models that look similar to this:

    # models.py of an app called  "articles"
    
    from core.models import UUIDModel, ActivatedModel
    from tasks.models import ManagedTasksModel
    
    class Article(UUIDModel, ActivatedModel, ManagedTasksModel):
        title = models.CharField()
        # blah...
    

    If an app grows too big I “micromanage” the app by breaking the models.py module into
    smaller modules following the above mentioned rules. I have found out that this covers
    most of my needs.

    I can’t comment on class based views, because honestly I don’t like them and it almost
    always make me write more code instead of less. To each his own. I prefer to use helper
    utility functions and make good use of things like context processors, inside
    my view functions.

    I hope my answer was within the context of your question.

    EDIT: I just noticed your usage of related_name which I think misses the point of that option. See the following example:

    class Message(models.Model):
        sender   = models.ForeignKey(User, related_name='messages_sent')
        receiver = models.ForeignKey(User, related_name='messages_received')
        body     = models.Textfield()
    

    With the above model we can do this, which is very readable:

    u1 = User.objects.get(...)
    received = u1.messages_received.all()
    

    … and it depicts the functional purpose of this relationship. So related_name is not
    only used for having related names which are unique.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to run a str_replace or preg_replace which looks for certain words
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping 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.