Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. class Solution(models.Model):
  2. '''
  3. Represents a solution to a specific problem.
  4. '''
  5. name = models.CharField(max_length=50)
  6. problem = models.ForeignKey(Problem)
  7. description = models.TextField(blank=True)
  8. date = models.DateTimeField(auto_now_add=True)
  9.  
  10. class Meta:
  11. unique_together = ("name", "problem")
  12.  
  13. class SolutionForm(forms.ModelForm):
  14. class Meta:
  15. model = Solution
  16. exclude = ['problem']
  17.  
  18. def validate_unique(self):
  19. exclude = self._get_validation_exclusions()
  20. exclude.remove('problem') # allow checking against the missing attribute
  21.  
  22. try:
  23. self.instance.validate_unique(exclude=exclude)
  24. except ValidationError, e:
  25. self._update_errors(e.message_dict)
  26.  
  27. class SolutionForm(forms.ModelForm):
  28. class Meta:
  29. model = Solution
  30. exclude = ['problem']
  31.  
  32. def clean(self):
  33. cleaned_data = self.cleaned_data
  34.  
  35. try:
  36. Solution.objects.get(name=cleaned_data['name'], problem=self.problem)
  37. except Solution.DoesNotExist:
  38. pass
  39. else:
  40. raise ValidationError('Solution with this Name already exists for this problem')
  41.  
  42. # Always return cleaned_data
  43. return cleaned_data
  44.  
  45. class SolutionForm(forms.ModelForm):
  46.  
  47. class Meta:
  48. model = Solution
  49. exclude = ['problem']
  50.  
  51. def clean(self):
  52. cleaned_data = self.cleaned_data
  53. if Solution.objects.filter(name=cleaned_data['name'],
  54. problem=self.problem).exists():
  55. raise ValidationError(
  56. 'Solution with this Name already exists for this problem')
  57.  
  58. # Always return cleaned_data
  59. return cleaned_data
  60.  
  61. class SolutionForm(forms.ModelForm):
  62. class Meta:
  63. model = Solution
  64. exclude = ['problem']
  65.  
  66. def _get_validation_exclusions(self):
  67. exclude = super(SolutionForm, self)._get_validation_exclusions()
  68. exclude.remove('problem')
  69. return exclude
  70.  
  71. def your_view(request):
  72. if request.method == 'GET':
  73. form = SolutionForm()
  74. elif request.method == 'POST':
  75. problem = ... # logic to find the problem instance
  76. solution = Solution(problem=problem) # or solution.problem = problem
  77. form = SolutionForm(request.POST, instance=solution)
  78. # the form will validate because the problem has been provided on solution instance
  79. if form.is_valid():
  80. solution = form.save()
  81. # redirect or return other response
  82. # show the form
  83.  
  84. class Solution(models.Model):
  85. ...
  86. def save(self, *args, **kwargs):
  87. self.clean()
  88. return super(Solution, self).save(*args, **kwargs)
  89.  
  90.  
  91. def clean():
  92. # have your custom model field checks here
  93. # They can raise Validation Error
  94.  
  95. # Now this is the key to enforcing unique constraint
  96. self.validate_unique()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement