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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:10:27+00:00 2026-06-11T16:10:27+00:00

the class: class Operation(models.Model): related_operation = models.ForeignKey(‘self’, null = True) __related_operation = None def

  • 0

the class:

class Operation(models.Model):
  related_operation = models.ForeignKey('self', null = True)

  __related_operation = None

  def __init__(self, *args, **kwargs):
    super(Operation, self).__init__(*args, **kwargs)
    self.__related_operation = self.related_operation

  def save(self, force_insert=False, force_update=False):
    if self.related_operation != self.__related_operation:
      del self.__related_operation

    super(Operation, self).save(force_insert, force_update)
    self.__related_operation = self.related_operation

the code:

 (...)
 OperationFormSet = modelformset_factory(Operation, form=OperationCategoryOnlyForm)
  if request.method == "POST":
    formset = OperationFormSet(request.POST, queryset=Operation.objects.filter(category=category_id))
    if formset.is_valid():
      instances = formset.save(commit = False)
      for instance in instances:
        if instance.related_account is not None:
          related_operation = Operation()
          related_operation.related_operation = instance
          related_operation.save()
          instance.related_operation = related_operation

        instance.save()

      formset = OperationFormSet(queryset=Operation.objects.filter(category=category_id))
  else:
    formset = OperationFormSet(queryset=Operation.objects.filter(category=category_id))

and the problem:

without the overwriting the method save everything works properly, but if i change related_operation in form, it of course makes second, third and so on related operation.

so i decided to overwrite the save method – and add the __related_operation. During saving it checks if related_operation changes – if so – it should delete the old related_operation and than save (with creating a completely new one related_operation)… And it almost works! (the new related_operation is added to database, but it loops after save. It never ends. I don’t know what it is doing. I have to restart apache to get any answer from server. Maybe it’s stupid mistyping or something very complex – i’m stacked…

(of course the operation class has more than one field e.g. related_account, so the if instance.related_account is not None: is correct)

UPDATE:

I found, that the problem is here:

def __init__(self, *args, **kwargs):
  super(Operation, self).__init__(*args, **kwargs)
  #self.__related_operation = self.related_operation

If i comment out the line above – everything goes ok – but of course it is wrong – why it blocks sth.

  • 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-11T16:10:28+00:00Added an answer on June 11, 2026 at 4:10 pm

    I found where’s the problem and found the solution…

    I tried to link object of the same type in init – so during initialization I create another instance of the object so it creates another and another until it dies.

    So the solution (for now – maybe someone presents best) is:

    class Operation(models.Model):
      related_operation = models.ForeignKey('self', null = True)
    
      __related_operation = None
    
      def __init__(self, *args, **kwargs):
        super(Operation, self).__init__(*args, **kwargs)
        self.__related_operation = self.related_operation_id
    
      def save(self, force_insert=False, force_update=False):
        super(Operation, self).save(force_insert, force_update)
        if self.related_operation is not None:
          if self.related_operation_id != self.__related_operation:
            if self.__related_operation is not None:
             Operation.objects.get(id = self.__related_operation).delete()
    
        if self.related_operation is not None:
          self.__related_operation = self.related_operation_id
    
      def __unicode__(self):
        return str(self.id)
    

    as you can see – now I use the id instead of object, so assignment doesn’t fire the creation of object…

    I think this can be helpful for someone else.

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

Sidebar

Related Questions

class myClass (models.Model): related_operation = models.ForeignKey('self', null = True) __related_operation = None def save(self,
Here's my code: models: class Category(models.Model): name = models.CharField(max_length=100) def get_month_sum_series(self): return [('2012-01-01',100)] class
Hi I have the following models class Student(models.Model): student_id = models.CharField(max_length=3, unique=True) first_name =
Suppose I have this model (not real code): class Message(models.Model): content = models.CharField(...) mentions
I have a blog-like application with stories and categories: class Category(models.Model): ... class Story(models.Model):
I have a model based on Sequel and Oracle adapter: class Operation < Sequel::Model(DB[:operations])
class OperationCategoryOnlyForm(forms.ModelForm): class Meta: model = Operation fields = ('operation_type', 'category', 'related_account', ) ordering
I have this structure of model objects: Class A: b = models.ManyToManyField(B) Class B:
I have the following model: class CompanyReport(models.Model): company = models.CharField(max_length=300) desc = models.TextField() text
I have a class that provide the CRUD operation for an entity.Im using 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.