Created
September 4, 2018 20:55
-
-
Save vulcan25/0e75f04b2f7693a7aa27e6e214fce4af to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# models.py | |
# | |
class Claim(models.Model): | |
week_commencing = models.DateField() | |
tech = models.ForeignKey( | |
settings.AUTH_USER_MODEL, | |
on_delete=models.PROTECT, | |
) | |
def __str__(self): | |
return '%s @%s' % (self.week_commencing,self.tech) | |
class Journey(models.Model): | |
claim = models.ForeignKey('Claim',on_delete=models.PROTECT,related_name='journeys') | |
date = models.DateField() # Date of the actual journey | |
distance = models.IntegerField() # in miles | |
# | |
# forms.py | |
# | |
class ClaimForm(forms.ModelForm): | |
class Meta: | |
model = Claim | |
fields = ['week_commencing'] | |
class JourneyForm(forms.ModelForm): | |
class Meta: | |
model = Journey | |
fields = ['date', 'distance','claim'] | |
# | |
# views.py | |
# | |
# views -> Claims | |
class ClaimListView(ListView): | |
model = Claim | |
def get_queryset(self): # Only list claims which belong to the user | |
return Claim.objects.filter(tech=self.request.user) | |
class ClaimCreateView(CreateView): | |
model = Claim | |
form_class = ClaimForm | |
def form_valid(self, form): # Automatically set the user on form submission | |
form.instance.tech = self.request.user | |
return super(ClaimCreateView, self).form_valid(form) | |
class ClaimDetailView(DetailView): | |
model = Claim | |
def get_queryset(self): # Only list items which belong to the user | |
return Claim.objects.filter(tech=self.request.user) | |
class ClaimUpdateView(UpdateView): | |
model = Claim | |
form_class = ClaimForm | |
# views -> Journeys | |
class JourneyListView(ListView): | |
model = Journey | |
def get_queryset(self): # Only list items which belong to the user | |
return Journey.objects.filter(claim__tech=self.request.user) | |
class JourneyCreateView(CreateView): | |
model = Journey | |
form_class = JourneyForm | |
class JourneyDetailView(DetailView): | |
model = Journey | |
def get_queryset(self): # Only list items which belong to the user | |
return Journey.objects.filter(claim__tech=self.request.user) | |
class JourneyUpdateView(UpdateView): | |
model = Journey | |
form_class = JourneyForm |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment