Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. function getOffers(key){
  2. dict = {
  3. 'key':key// pk value of post sent to retrieve offers to it
  4. };
  5. key = "pk";//I actually dont know what to put here
  6. generateCSRFToken();
  7. $.ajax({
  8. url: "/retrieve_offers/",
  9. method: "POST",
  10. data : JSON.stringify(dict),
  11. success: function(data){
  12. data = JSON.parse(data);
  13. Object.keys(data).forEach(function(key){
  14.  
  15. $("#offercontainer").html(
  16. "<p>" + key + "</p>"
  17. )
  18.  
  19. });
  20. },
  21. error: function(){
  22.  
  23. }
  24. })
  25.  
  26. def retrieve_offers(request):
  27. dict = json.loads(request.body)
  28. post_key = Post.objects.get(pk=dict["key"])
  29. offers = Offer.objects.filter(post_id=post_key)
  30.  
  31. data = serializers.serialize('json',offers)
  32.  
  33. return HttpResponse(
  34. json.dumps(data),
  35. content_type="application/json"
  36. )
  37.  
  38. class Post(models.Model):
  39. date_submitted = models.DateField()
  40. description = models.CharField(max_length=1024)
  41. author = models.ForeignKey(to=User, on_delete=models.CASCADE)
  42. category = models.ForeignKey(to=Category,on_delete=models.CASCADE)
  43. trade_item = models.FileField(null=True,blank=True)
  44. trade_item_name = models.CharField(null=True,max_length=100)
  45. receive_item = models.FileField(null=True, blank=True)
  46. receive_item_name = models.CharField(null=True,max_length=100)
  47.  
  48. def __str__(self):
  49. return str(self.pk) + " by " + (self.author.username)
  50.  
  51. @staticmethod
  52. def create(date_submitted, description, author, category):
  53. post = Post()
  54. post.date_submitted = date_submitted
  55. post.description = description
  56. post.author = author
  57. post.category = category
  58. return post
  59.  
  60. class Offer(models.Model):
  61. date_submitted = models.DateField()
  62. description = models.CharField(max_length=1024)
  63. author = models.ForeignKey(to=User,on_delete=models.CASCADE)
  64. post_id = models.ForeignKey(to=Post,on_delete=models.CASCADE)
  65. image = models.FileField(null=True, blank=True)
  66. item_name = models.CharField(null=True,max_length=100)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement