Opened 7 years ago

Closed 3 years ago

#6818 closed Bug (duplicate)

Generic Relations, doesn't seem to delete all related objects.

Reported by: niklas Owned by: nobody
Component: contrib.contenttypes Version: master
Severity: Normal Keywords: GenericForeignRelation, deletion
Cc: niklas@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Django doesn't seem to delete all related objects.
Below: an owner has property has 2 resource.
Deleting the owner will delete the property but not the resources

class Property(models.Model):
    owner = generic.GenericForeignKey()        
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()

class Resource(models.Model):
    property = models.ForeignKey(Property, edit_inline=models.STACKED, num_in_admin=10) 
    language_id = models.IntegerField(default=1, core=True)
    class Meta:
        unique_together = (("property", "language_id"),)   

class AnExampleOwner(models.Model):
    properties = generic.GenericRelation(Property) 

An example from the interpreter

In [2]: AnExampleOwner.objects.all().count()
Out[2]: 0L

In [3]: Property.objects.all().count()
Out[3]: 0L

In [4]: Resource.objects.all().count()
Out[4]: 0L

In [5]: o=AnExampleOwner()

In [6]: o.save()

In [7]: p=Property(owner = o)

In [8]: p.save()

In [9]: r=Resource(property=p, language_id = 1)

In [10]: r.save()

In [11]: r=Resource(property=p, language_id = 2)

In [12]: r.save()

In [13]: AnExampleOwner.objects.all().count()
Out[13]: 1L

In [14]: Property.objects.all().count()
Out[14]: 1L

In [15]: Resource.objects.all().count()
Out[15]: 2L

In [16]: o.delete()

In [17]: AnExampleOwner.objects.all().count()
Out[17]: 0L

In [18]: Property.objects.all().count()
Out[18]: 0L

In [19]: Resource.objects.all().count()
Out[19]: 2L

Attachments (1)

6818-failing-tests-against-8818.diff (1.9 KB) - added by Honza_Kral 7 years ago.
tests that fail for the current SVN version

Download all attachments as: .zip

Change History (8)

comment:1 Changed 7 years ago by niklas

  • Cc niklas@… added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Further results: Deleting a property directly will delete the resources connected to it (i.e. that works correctly)

comment:2 Changed 7 years ago by telenieko

  • Summary changed from Django doesn't seem to delete all related objects. to Generic Relations, doesn't seem to delete all related objects.
  • Triage Stage changed from Unreviewed to Accepted

Changed 7 years ago by Honza_Kral

tests that fail for the current SVN version

comment:3 Changed 4 years ago by julien

  • Severity set to Normal
  • Type set to Bug

comment:4 Changed 4 years ago by anonymous

  • Component changed from Database layer (models, ORM) to contrib.contenttypes

comment:5 Changed 4 years ago by aaugustin

  • UI/UX unset

Change UI/UX from NULL to False.

comment:6 Changed 4 years ago by aaugustin

  • Easy pickings unset

Change Easy pickings from NULL to False.

comment:7 Changed 3 years ago by ramiro

  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of (newer, already fixed) #12953.

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