Guest User

Untitled

a guest
Apr 20th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. class Competition(models.Model):
  2. title = models.CharField(max_length=256)
  3.  
  4. class Gymnast(models.Model):
  5. parent = models.ForeignKey(Competition, related_name='gymnasts')
  6. name = models.CharField(max_length=256)
  7.  
  8. class Result(models.Model):
  9. parent = models.ForeignKey(Gymnast, related_name='results')
  10.  
  11. score1 = models.FloatField(blank=True, null=True)
  12. score2 = models.FloatField(blank=True, null=True)
  13. score3 = models.FloatField(blank=True, null=True)
  14. score4 = models.FloatField(blank=True, null=True)
  15. score5 = models.FloatField(blank=True, null=True)
  16. score6 = models.FloatField(blank=True, null=True)
  17. score7 = models.FloatField(blank=True, null=True)
  18.  
  19. class YourModelForm(forms.ModelForm):
  20.  
  21. extra_field = forms.CharField()
  22.  
  23. def save(self, commit=True):
  24. extra_field = self.cleaned_data.get('extra_field', None)
  25. # ...do something with extra_field here...
  26. return super(YourModelForm, self).save(commit=commit)
  27.  
  28. class Meta:
  29. model = YourModel
  30.  
  31. class PersonForm(forms.ModelForm):
  32. continent = forms.ChoiceField(choices=CONTINENT_CHOICES)
  33.  
  34. class Meta:
  35. model = Person
  36. fields = ('__all__')
  37. widgets = {
  38. 'birth_country': autocomplete.ModelSelect2(url='country-autocomplete',
  39. forward=['continent'])
  40. }
  41.  
  42. class CountryAutocomplete(autocomplete.Select2QuerySetView):
  43.  
  44. def get_queryset(self):
  45. if not self.request.user.is_authenticated():
  46. return Country.objects.none()
  47.  
  48. qs = Country.objects.all()
  49.  
  50. continent = self.forwarded.get('continent', None)
  51.  
  52. if continent:
  53. qs = qs.filter(continent=continent)
  54.  
  55. if self.q:
  56. qs = qs.filter(name__istartswith=self.q)
  57.  
  58. return qs
Add Comment
Please, Sign In to add comment