Login

Self-referencing Foreign Key Infinite Loop

Author:
minarets
Posted:
September 17, 2007
Language:
Python
Version:
.96
Score:
2 (after 2 ratings)

Make sure we don't create an infinite loop with a self-referencing foreign key.

Many times I have needed category models that reference themselves, providing a flexible way to make children categories, grandchildren categories, etc. If you chain the slugs (or even ids) there's a chance you could end up with an infinite loop.

Assumes a required, unique slug field ('slug') and an optional self-referencing foreign key ('parent'). All_data doesn't give you the object's ID, so we will find it via the unique slug. If either is not present we pass -- if there's no parent chosen it's not a problem, and if there is no slug present the submission will fail on that validation instead. It is worth noting that if the user changes the slug field on submission AND picks a bad parent it will not be caught.

Infinite loop cases:

  1. A references A
  2. A tries to reference B, which is currently referencing A
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
def anti_loop(field_data, all_data):
    """
    Make sure we don't create an infinite loop with a
    self-referencing foreign key.
    
    Assumes a required, unique slug field ('slug') and an optional 
    self-referencing foreign key ('parent'). All_data doesn't give 
    you the object's ID, so we will find it via the unique slug.
    If either is not present we pass -- if there's no parent chosen 
    it's not a problem, and if there is no slug present the 
    submission will fail on that validation instead. It is worth 
    noting that if the user changes the slug field on submission AND 
    picks a bad parent it will not be caught.
    
    Infinite loop cases:
    1.    A references A
    2.    A tries to reference B, which is currently referencing A
    """
    
    parent = all_data.get('parent', '')
    slug = all_data.get('slug', '')
    error_msg = u"Please chose a parent that does not result in an infinite loop!"
    if parent and slug:
        try:
            this_category = Category.objects.get(slug = slug)
            parent_category = Category.objects.get(pk = int(parent))
            if parent_category.id == this_category.id or parent_category.parent == this_category:
                raise ValidationError(error_msg)                
        except ObjectDoesNotExist:
            pass

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 9 months ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 9 months, 1 week ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 4 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 4 months ago
  5. Help text hyperlinks by sa2812 1 year, 5 months ago

Comments

Please login first before commenting.