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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:56:45+00:00 2026-05-26T13:56:45+00:00

I have 2 models.py files in different app directories: users.models.py and friends.models.py. There is

  • 0

I have 2 models.py files in different app directories: users.models.py and friends.models.py.

There is one problem: if some user deleted from UserProfile model, all his friendship network must be deleted too with him. It is very natural.

But when I import Frienship to users.model.py I’ve got an error: Cannot import name Friendship

I understand an error arose because of mutual importing in these 2 files, and I know I can easely solve this problem with the help of signals, but I do not know how to do it in proper way.

Could anybody help in this particular case?

In users.models.py:

from friends.models import Friendship

class UserProfile(models.Model):
    username = models.Charfield(max_length=50)
    ...

    def delete(self, *args, **kwargs):
        Friendship.objects.remove_all(self)
        self.delete(*args, **kwargs)

In friends.models.py:

from users.models import UserProfile

class FriendshipManager(models.Manager):
    def remove_all(self, user):
        usr = Friendship.objects.get(user=user).friends
        frs = [i.user for i in usr.all()]
        for fr in frs:
            usr.remove(fr)

class Friendship(models.Model):
    user = models.Foreignkey(UserProfile)
    friends = models.ManyToManyField('self')

    objects = FriendshipManager()

Thanks in advance!!!

  • 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-26T13:56:46+00:00Added an answer on May 26, 2026 at 1:56 pm

    No need for signals, just use the magic *_set property. It will be defined at runtime so you don’t have to worry about circular imports.

    friends/models.py

    from users.models import UserProfile
    
    class Friendship(models.Model):
        user = models.Foreignkey(UserProfile)
        friends = models.ManyToManyField('self')
    

    users/models.py

    class UserProfile(models.Model):
        username = models.Charfield(max_length=50)
        ...
    
        def delete(self, *args, **kwargs):
            for f in self.friendship_set.all():
                f.delete()
            super(self.__class__, self).delete(*args, **kwargs)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that splits models into different files. Actually the folder looks
I have a django app with some model for uploading files. I need to
I currently have a Rails app in which users of different ranks can upload
I have a few things in various different files that get called from different
In a Rails 3.2 app I have two user models set up using Devise:
I have a Python project with following directory structure: /(some files) /model/(python files) /tools/(more
I've just noticed that my app is including over 148 php files on one
I have an app where I want to have one background view (view controller
I have one .net application which would be installed under program files running in
I have one HTML file located in app/views/layouts/application.html.erb I use <%=yield=> to render the

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.