Opened 14 years ago

Last modified 14 years ago

#14295 closed

Saving an object with a custom manager results in IntegrityError — at Version 1

Reported by: loekje Owned by: nobody
Component: Uncategorized Version: 1.0
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Alex Gaynor)

I have defined customer manager that filters out certain object (e.g. those objects marked as inactive).
The customer manager is the first manager defined in the model, consequently it is used as the default manager for this model.
Additionally, I have defined another manager for the model that does no filtering (i.e. the standard manager).

When properties of an inactive object are updated and the inactive object is saved, an IntegrityError is raised. This is because the default manager cannot find the object and attempts to insert the object again instead of updating it.

class MyModelManager(models.Manager):
   def get_query_set(self):
      return super(MyModelManager, self).get_query_set().filter(is_active=True)

class MyModel: models.Model
   name=CharField
   ... some additional properties ...
   is_active=BooleanField

   objects=MyModelManager()
   objects_all=models.Manager()

m=MyModel()
m.name='John'
m.is_active=False
m.save()

m.name='John Doe'
m.save()
--> IntegrityError!!!

I would like that the default manager can be set such that inactive objects are not shown (i.e. also not when using mymodel_set on related objects) such that by default the inactive objects are not taken into account. However, to avoid the IntegrityError when saving an object I would like to be capable of overruling a manager that has to be used by save (and most likely also by delete), in the model and/or as in argument in the save call.

Change History (1)

comment:1 by Alex Gaynor, 14 years ago

Description: modified (diff)

I've reformatted the description of this ticket, in the future you can use the "Preview" button to ensure that your message is formatted the way you intended.

Note: See TracTickets for help on using tickets.
Back to Top