Guest User

Untitled

a guest
Feb 25th, 2018
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. from django.db.models import CharField, Model, OneToOneField, AutoField, EmailField
  2.  
  3.  
  4. class User(Model):
  5. id = AutoField(primary_key=True) # The default Id field from django
  6. username = CharField(max_length=30)
  7. profile = OneToOneField(Profile, primary_key=True, related_name='user')
  8.  
  9.  
  10. class Profile(Model):
  11. id = AutoField(primary_key=True) # The default Id field from django
  12. primary_email = EmailField()
  13. secondary_email = EmailField()
  14.  
  15.  
  16. def bulk_insert(list_of_user_details):
  17. """ Demonstrates the N+1 problem using a bulk creation of users example
  18.  
  19. Questions to think about:
  20. - Why does the first code block create a ValueError?
  21. - We can use bulk_create on the User model, but not the Profile model or both at once. Why not?
  22. - What happens when instead of 2 users, the bulk_insert is done for 10 users? 20,000 users? 10m users?
  23. - What happens when there's an error halfway through the insertion process?
  24.  
  25. """
  26.  
  27. # First of all, why does the following produce a ValueError like:
  28. # Traceback (most recent call last):
  29. # ...
  30. # ValueError: save() prohibited to prevent data loss due to unsaved related object 'profile' ?
  31. try:
  32. # Why does this produce a value error?
  33. user_details = list_of_user_details[0]
  34. user = User(username=user_details['username'])
  35. profile = Profile(primary_email=user_details['primary_email'], secondary_email=user_details['secondary_email'])
  36. user.profile = profile
  37. user.save()
  38.  
  39. except ValueError:
  40. pass
  41.  
  42. # Django has bulk creation for models... Let's just add them all at once! But why won't the following work?
  43. try:
  44. users = []
  45. for user_details in list_of_user_details:
  46. user = User(username=user_details['username'])
  47. user.profile = Profile(primary_email=user_details['primary_email'], secondary_email=user_details['secondary_email'])
  48. users.append(user)
  49. User.objects.bulk_create(users)
  50.  
  51. except ValueError:
  52. pass
  53.  
  54. # The following works! But what's the problem with it?
  55. try:
  56. users = []
  57. for user_details in list_of_user_details:
  58. user = User(username=user_details['username'])
  59. profile = Profile(primary_email=user_details['primary_email'], secondary_email=user_details['secondary_email'])
  60. profile.save()
  61. user.profile = profile
  62. users.append(user)
  63. User.objects.bulk_create(users)
  64.  
  65. except ValueError:
  66. pass
  67.  
  68. # How can we get over the problem in the previous code block?
  69.  
  70.  
  71. bulk_insert([{'username': 'joebloggs', 'primary_email': 'joe@me.com', 'secondary_email': 'jb@gmail.com'},
  72. {'username': 'anniebloggs', 'primary_email': 'annie@me.com', 'secondary_email': 'ab@gmail.com'}])
Add Comment
Please, Sign In to add comment