Advertisement
Guest User

Untitled

a guest
Sep 25th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.40 KB | None | 0 0
  1. Django/Python Practice Questions
  2. ========================
  3.  
  4. 1. Find the most frequent integer in a list, Test list : [3,5,6,3,9,0,3,8,3,1,3]
  5. 2. Given two tables:
  6.  
  7. ```python
  8. class Stream(models.Model):
  9. name = models.CharField(max_length=100)
  10. short_name = models.CharField(max_length=255, null=True, blank=True)
  11.  
  12. class Specialization(models.Model):
  13. name = models.CharField(max_length=100)
  14. stream = models.ForeignKey(Stream, null=True, blank=True)
  15. ```
  16. write django query to select all specialization along with stream using select related.
  17. if stream was a many to many key, how would you you change your query ?
  18. 3. Create a method on Stream model which should return only deleted objects (Hint: User model manager)
  19.  
  20. ```python
  21. class Stream(models.Model):
  22. name = models.CharField(max_length=100)
  23. short_name = models.CharField(max_length=255, null=True, blank=True)
  24. deleted = models.BooleanField(default=False)
  25. ```
  26. 4. Find the common elements of 2 lists
  27.  
  28. ```python
  29. a = [2,3,4,1,1,3,6]
  30. b = [2,8,9,1,3]
  31. ```
  32. find integers which are in list a but not list b
  33. do not use loops !
  34.  
  35. 5. Consider the Stream model, create a Form for this model and restrict the user from uploading any images other than PNG, throw a validation error
  36.  
  37. ```python
  38. class Stream(models.Model):
  39. name = models.CharField(max_length=100)
  40. short_name = models.CharField(max_length=255, null=True, blank=True)
  41. icon = models.ImageField(upload_to='img/appicons/', verbose_name = "Stream Icon")
  42. deleted = models.BooleanField(default=False)
  43. ```
  44. 6. Consider Following models, write django ORM query to get latest activity for each user
  45.  
  46. ```python
  47. class UserProfile(models.Model):
  48. name = models.CharField(max_length=100)
  49. email = models.EmailField(max_length=255, unique=True)
  50. username = models.CharField(max_length=100, unique=True)
  51. added_on = models.DateTimeField(auto_now_add=True)
  52. updated_on = models.DateTimeField(auto_now=True)
  53.  
  54. class UserActivity(models.Model):
  55. user = models.ForeignKey(UserProfile)
  56. source_url = models.URLField(max_length=400, null=True, blank=True)
  57. added_on = models.DateTimeField(auto_now_add=True)
  58. updated_on = models.DateTimeField(auto_now=True)
  59. ```
  60.  
  61. 7. Sort this list of tuples by second item in each tuple
  62.  
  63. ```python
  64. awesome_list = [(2,3,1),(6,8,6),(9,1,4),(4,4,0),(2,3,3),(6,2,1)]
  65. ```
  66.  
  67. 8. How do you export data from one database to another having the same model but with no data ? lets say we have a database A with model AppDetails, how do you export data of this model to another database B having same model
  68.  
  69. 9. Difference between StackedInline and TabularInline, if we want to add a model having Manytomany foreign key in django admin, which type of admin model should we use ?
  70.  
  71. 10. Write Django ORM query to find maximum salary of each person having name 'Pinky' or 'Rohit' excluding those which have 0 salary in the folowing model with test data
  72.  
  73. ```python
  74. class UserProfile(models.Model):
  75. name = models.CharField(max_length=100)
  76. email = models.EmailField(max_length=255, unique=True)
  77. username = models.CharField(max_length=100, unique=True)
  78. added_on = models.DateTimeField(auto_now_add=True)
  79.  
  80. class L2Salary(models.Model):
  81. user = models.ForeignKey(UserProfile)
  82. salary = models.FloatField()
  83. added_on = models.DateTimeField(auto_now_add=True)
  84. ```
  85.  
  86. Data in above models:
  87.  
  88. ```python
  89. userprofile = [
  90. {
  91. "added_on": "24-01-01 20:20",
  92. "email": "kuldeep@gmail.com",
  93. "id": 1,
  94. "name": "Kuldeep",
  95. "username": "Kully"
  96. },
  97. {
  98. "added_on": "24-01-02 20:20",
  99. "email": "mandeep@gmail.com",
  100. "id": 2,
  101. "name": "Mandeep",
  102. "username": "mandy"
  103. },
  104. {
  105. "added_on": "24-02-01 20:20",
  106. "email": "rohit@gmail.com",
  107. "id": 3,
  108. "name": "Rohit",
  109. "username": "irohit"
  110. },
  111. {
  112. "added_on": "24-02-01 20:20",
  113. "email": "pinky@gmail.com",
  114. "id": 4,
  115. "name": "Pinky",
  116. "username": "pinky"
  117. }
  118. ]
  119. l2salary = [
  120. {
  121. "added_on": "19-08-08 01:23",
  122. "id": 1,
  123. "salary": 24000.0,
  124. "user_id": 1
  125. },
  126. {
  127. "added_on": "19-08-08 01:23",
  128. "id": 2,
  129. "salary": 29300.0,
  130. "user_id": 1
  131. },
  132. {
  133. "added_on": "19-08-08 01:23",
  134. "id": 3,
  135. "salary": 21400.0,
  136. "user_id": 2
  137. },
  138. {
  139. "added_on": "19-08-08 01:23",
  140. "id": 4,
  141. "salary": 0.0,
  142. "user_id": 3
  143. },
  144. {
  145. "added_on": "19-08-08 01:23",
  146. "id": 5,
  147. "salary": 12000.0,
  148. "user_id": 4
  149. },
  150. {
  151. "added_on": "19-08-08 01:23",
  152. "id": 6,
  153. "salary": 32000,
  154. "user_id": 3
  155. },
  156. {
  157. "added_on": "19-08-08 01:23",
  158. "id": 7,
  159. "salary": 0.0,
  160. "user_id": 3
  161. },
  162. {
  163. "added_on": "19-08-08 01:23",
  164. "id": 8,
  165. "salary": 23400.0,
  166. "user_id": 4
  167. }
  168. ]
  169. ```
  170.  
  171. 11. Bonus Question: Write a middleware that restricts only single session per user, if i am logged in from one browser at one place, and if i try to login from any other place using same credentials, it should log out the older session and log me in with current session.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement